Remove or add www to URL using Apache mod_rewrite

To avoid a website being considered as "duplicate content" by Google (i.e. the same website appearing under different URLs) it is a good idea to make sure the website doesnt appear as both http://www.example.com and http://example.com. This can be achieved by using the mod_rewrite in Apache using the .htaccess file.

To remove "www" from the URL, i.e. the website’s URL should be http://example.com, your .htaccess should look like this: 

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

If you instead want to make sure "www" is added, i.e. the website’s URL should be http://www.example.com, your .htaccess should look like this: 

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

 

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

16 + 4 =