Setting up canonical URL as www or non-www

Last updated on December 15, 2009. Tags: , , , , , ,

A webpage can be accessed through two or more different URLs. The most common is the www vs non-www URL. For example, http://domain.com/about.htm and http://www.domain.com/about.htm will always (as far as all websites I encountered) lead to the same web page. There are other strings that can cause a web page to have more than one valid URL ("valid" means typing the URL will lead to a specific page) such as session IDs and trailing slash but those will be discussed in some of my future posts.

A canonical URL is the one preferred by the webmaster over several different URLs. In this particular discussion, which URL does the webmaster prefer, the one with www or the one without www? While the webmaster can pick one over the other and build internal links and backlinks consistently, s/he cannot control how other people link to his/her site. S/he can build his/her forum signatures with non-www format but if someone saw his/her post and decided to link it, that person might decide to link using the URL with www.

This can cause problems with regards to search engine optimization (SEO). The search engines will see domain.com and www.domain.com as two different websites, and domain.com/page.htm and www.domain.com/page.htm as two different webpages. When they see that these two different URLs have exactly the same content, they will impose duplicate content penalty on one or both of your pages. Another problem is that the inbound links are divided between the two versions of the URL; if you have 100 inbound links but 50 of them are using the non-www and the others using the one with www, search engines will see them as 50 inbound links to two different webpages instead of 100 inbound links to just one webpage.

The solution to this problem is to redirect all incoming traffic to your preferred or canonical URL. Let's say you choose the non-www format. If the user typed www.domain.com/about.htm, s/he will be redirected to domain.com/about.htm and the URL in his/her address bar will change. If another website links to your page as www.domain.com, when someone clicked on that link, s/he will still be directed to domain.com.

The first step is to choose which URL format you prefer, the one with www or the one without www. Then, open your .htaccess file and insert either of the following codes based on your choice.

If your chosen format is the one with www, insert this code:

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

If your chose the non-www format, insert the code below:

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

Note that these codes also work with subdomains. Simply replace either "domain" or "domain2" with "subdomain.domain". You can also replace the .com with any top level domain (TLD) like .net, .ph or .com.ph.

Posted by Greten on November 28, 2009 under Search Engine Optimization (SEO), Server Configuration

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati

Related Posts

You might also be interested (randomly generated):

Post Comments

Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.





* Required. Your email will never be displayed in public.