Setting Your Canonical Domain

by dave vogler
labyrinth

You may have noticed that most websites are accessible two ways: with or without the 'www.' prepended to the domain name. Both URLs serve the same content, but one of them should be declared the 'primary', or 'canonical' URL.


For example:

http://sparkhouse.com
http://www.sparkhouse.com

If you've got two different URLs that lead to your site, there are effectively two websites available with exactly the same content. People as well as search engines notice this. While Google assures us that non-malicious duplicate content will not carry a search rank penalty, it is still good practice to indicate which domain you prefer. This 'preferred' version of the domain is called 'canonical', and it also applies to links within your site. If you don't declare which URLs are your preferred URLs, the search engine will do it for you

There is an easy way to declare your canonical domain using your .htaccess file (assuming your web server is Apache and you have mod_rewrite enabled)
Use the following rule to redirect all non-www urls to a url with www, as in sparkhouse.com-->www.sparkhouse.com

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

Use the following rule to affect the opposite: redirect all www-urls to a non-www url, as in sparkhouse.com-->www.sparkhouse.com.

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

If you are successful, you'll see the redirect happen when you type in the other domain. The search engines will see the same thing, and soon your pages will index they way you want them to!

Dave Vogler is a design geek and web developer in Rochester, New York. He likes to build websites with Drupal, but has a soft spot for hand-crafted markup.
You can usually find him on twitter.
categories:

Comments

Post new comment