How to redirect non-www to www, and www to non-www

Have you ever wanted to force the domain to be viewed with (or without) the www in front?

Here’s a simply way to do so. You just have to add the following lines in the .htaccess of your public_html for the domain you want this done on.

To change from www to non-www (This means when someone goes to http://www.yourdomain.com, they will be redirected to http://yourdomain.com):

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule ^(.*)://yourdomain.com/ [L,R=301]

To change from non-www to www (This means when someone goes to http://yourdomain.com, they will be redirected to http://www.yourdomain.com):

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)://www.yourdomain.com/ [L,R=301]

In both examples, replace yourdomain.com with your actual domain.

Leave a Reply

Your email address will not be published.