How to Add – Remove the WWW Prefix from Domain Name Using .htaccess


To avoid duplicate content problem with search engines, you have to redirect all pages to one common URL.

This you can achieve using mod_rewrite of apache and .htaccess.

To redirect any URL starting with www.domain.tld to domain.tld, you have to add the following code to .htaccess file.

Do a 301 redirect for all http requests that are going to the wrong url.

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domain\.tld$ [NC]
RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]
</IfModule>

or

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^domain.tld$ [NC]
RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]
</IfModule>

To redirect domain.tld to www.domain.tld

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain\.tld$ [NC]
RewriteRule ^(.*)$ http://www.domain.tld/$1 [R=301,L]
</IfModule>

or

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.tld$ [NC]
RewriteRule ^(.*)$ http://www.domain.tld/$1 [L,R=301]
</IfModule>

Related Posts:

2 Responses


Short Url Redirect November 16, 2009 at 4:16 pm

Please note the Google PageRank goes up to 10 and the best alexa rating is 1. Short Url Redirect


Namit Gupta November 16, 2009 at 4:16 pm

What if this is done at DNS level. How can it be changed?


Comments are closed.