Canonical Urls With Mod Rewrite
It's not unusual to have multiple domains pointing at one site, possibly for brand protection or vanity domains.
If these extra domains are simply added in as aliases of the main domain, they will all appear as separate sites to search engines such as Google or Yahoo. This means that the various domains probably incur a duplicate content penalty.
Fortunately, this is easy to get around. Presuming that your primary domain name is example.com, the following can be put at the top of the .htaccess file in your document root:
As written above, example.com will appear as the canonical url rather than www.example.com. If the user does enter www.example.com, they will get redirected to example.com. To change this behavior, add in the www in the third line.
¹HTTP_HOST is the domain name that the user is using to access the site when the .htaccess file is called by Apache.
If these extra domains are simply added in as aliases of the main domain, they will all appear as separate sites to search engines such as Google or Yahoo. This means that the various domains probably incur a duplicate content penalty.
Fortunately, this is easy to get around. Presuming that your primary domain name is example.com, the following can be put at the top of the .htaccess file in your document root:
The RewriteCond line checks to see if the HTTP_HOST¹ is NOT example.com. If this is the case it will do a permanent(301) redirect to http://example.com/$1, where $1 is the part that was after the HTTP_HOST.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
As written above, example.com will appear as the canonical url rather than www.example.com. If the user does enter www.example.com, they will get redirected to example.com. To change this behavior, add in the www in the third line.
¹HTTP_HOST is the domain name that the user is using to access the site when the .htaccess file is called by Apache.
0 TrackBacks
Listed below are links to blogs that reference this entry: Canonical Urls With Mod Rewrite.
TrackBack URL for this entry: http://www.modrewrite.co.uk/cgi-bin/mt/mt-tb.cgi/2402

Leave a comment