Home > htaccess > How do I redirect all links for www.domain.com to domain.com ?

How do I redirect all links for www.domain.com to domain.com ?

January 9th, 2010 Leave a comment Go to comments    

Description of the problem:

Your website can be accessed with www.domain.com and domain.com. Since Google penalizes this due to duplicated content reasons, you have to stick your domain to either www.domain.com or domain.com.
But – since some links are outside of your website scope and the search engines already have indexed your website under both addresses, you can’t change that easily.

Solution:

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

Example 1 – Redirect domain.com to www.domain.com

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

Example 2 – Redirect www.domain.com to domain.com

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

Categories: htaccess Tags:
  1. No comments yet.
  1. No trackbacks yet.