Redirect www to non www

My website is koml.ml and I wanna redirect www.koml.ml to koml.ml only when using www.koml.ml.
But if someone visit www.koml.ml/post them he should not redirect to koml.ml.

add this JS code in index page of your website.

if (!location.href.indexOf('http://www'))
    location.href = 'http://koml.ml';

It will redirect your site-only index page from www to non-www.

Also, creating URL shortener website is not allowed on free hosting service. It is the violation of TOS and your account will be suspended

Add this to your .htaccess code, do not use the JS code above as it is not effcient.

RewriteEngine On
RewriteCond %{HTTP_HOST} www.yourwebsitehere.com
RewriteRule (.*) http://yourwebsitehere.com/$1 [R=301,L]

Make sure to change www.yourwebsitehere.com to your website.

2 Likes

It will cause a redirect from any page to non-www whereas he asked for only index page whereas other pages should work under www.

1 Like

You can do that with .htaccess rules too.

I just doodled this based on @TigerMANEK426’s example. Please try if this works.

RewriteEngine On
RewriteCond %{HTTP_HOST} www.yourwebsitehere.com
RewriteRule ^/?$ http://yourwebsitehere.com/$1 [R=301,L]
2 Likes

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.