Domain Canonicalisation (Apache)

Monday, 21 September 2009

Hosting your website on multiple domain names can cause a duplicate content penalty. Many of us have variations of our domain names (such as and or and so we need to let the search engines know which one is the primary (canonical) domain using 301 redirects via an apache rewrite rule.

We want to automatically 301 redirect all requests like: to

AND to

First you need to enable the rewrite engine (you may already have this line in your .htaccess file so double check before you add it):

RewriteEngine on

For a single domain we redirect any request that begins to

RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

If you only have one domain this is all you need to do.

For two or more domains follow this format:

RewriteCond %{HTTP_HOST} ^ [NC,OR]
RewriteCond %{HTTP_HOST} ^(.*) [NC,OR]
RewriteCond %{HTTP_HOST} ^(.*) [NC,OR]
#–> Carry on adding as many of these lines as you need

RewriteCond %{HTTP_HOST} ^(.*) [NC]
#—————–>> Note there is no OR on the last condition^^^

RewriteRule ^(.*)$$1 [L,R=301]

It would also be wise to set the canonical URL in Google Webmaster tools.

We will post a guide on how to specify a canonical index page next week.