Redirect www to non www

My website is and I wanna redirect to only when using
But if someone visit them he should not redirect to

add this JS code in index page of your website.

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

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}
RewriteRule (.*)$1 [R=301,L]

Make sure to change to your website.


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}
RewriteRule ^/?$$1 [R=301,L]

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