This post isn’t about why you should have a canonical URL, but about how best to implement one. If you need convincing on the why argument, I’ll refer you to this informative post from Matt Cutts.

Let’s start with a simple example. You own example.com and you have decided that you want to make www.example.com your canonical URL. One valid approach to do this would be to add a RewriteRule to your .htaccess file testing the incoming request’s host name and if it is example.com, then redirect to www.example.com. This works, but it is not the best way to do it.

A better approach is to reverse the RewriteRule’s logic. Instead of testing for a specific host name you want to redirect, test that the incoming request’s host name is NOT your canonical URL. If you only have one domain, you may not see the benefit to this approach, but let’s say you have example.com, example.net and example.org and you want them to ALL funnel to the canonical www.example.com. With the first approach, you’d be writing 5 rules. With the second approach, you still only need the one rule. Even better, if you continue to buy additional domains, you don’t have to remember to alter your rewrite rules to keep up with your domain addiction.

Enough theory, let’s look at the specific rules you can use in your .htaccess file:

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

First, I assume you already have RewriteEngine on in your Apache configuration or .htaccess file.

The RewriteCond line is testing the incoming request and evaluating the host header to see what domain the user is requesting. Note the ! at the front of the rule. This means we will redirect if the incoming host header is NOT www.example.com. The [NC] flag just means that we don’t care if any capitalization is used. It will evaluate true whether it is Example.com or EXAMPLE.com.

The RewriteRule simply does a permanent redirect to our canonical URL, preserving the page name in the process.

Depending on your server configuration, you may also want to test SERVER_NAME. In practice on our server, I find this is not necessary, but your mileage may vary. If you’re curious, check out this explanation of the difference between HTTP_HOST and SERVER_NAME on Stack Overflow.

If you’re really curious, you can dig in to the Apache documents on mod_rewrite and learn more than you ever wanted to know on the topic.

6 thoughts on “The best way to declare a canonical URL

  1. Joel, in your post, you said:

    “…let’s say you have example.com, example.net and example.org and you want them to ALL funnel to the canonical http://www.example.com.”

    Can you tell me reason why someone might want three domain names to redirect in that fashion?

    Thanks,
    Nick

  2. In short, if all of these domains are displaying the same exact site, then search engines prefer it if you pick one as your “canonical” URL. It prevents your rankings from being diluted across multiple domains or, even worse, from being penalized for having “duplicate content.”

    If you want the full details, I would direct you to the link I put in the first paragraph to Matt Cutts’ blog.

  3. Good answer Joel. This is important, as we need to be careful not to confuse Google, and to make sure they know which is the right domain name to index. I’m all for consolidating “link juice and avoiding possible duplicate content penalization.

  4. Hi Joel,

    Good post as it’s definitely important to identify and control duplicity of content, as well as make people aware of what it is, and why you should use it. However, I was just curious about your example regarding example.com, .org, and .net and using canonical tags for it, as compared to 301 redirects?

    If you refer to Matt’s blog post, he was using the example of having multiple versions of one site or page (which is what canonical is used for), not multiple domains.

    So, if you are looking to have your base domain be example.com and you have the same content on .net and .org (perhaps you did it for branding purposes, etc.), then I believe it makes more sense to 301 those additional domains to your base domain (example.com) and set your canonicalization site-wide for that domain (so as to eliminate the canonicalization between “www” & “non-www” and “/” & “index.html, index.php, default.asp, etc.”).

    Cheers.

  5. Kyle,

    I am recommending the use of 301s to consolidate domains. If you look at my Apache redirect commands, I’m specifying [R=301], which means to issue a 301-based redirect.

    I believe the canonical “tag” (actually a link tag with a rel=”canonical” attribute) is more suited for individual pages on a domain that have multiple formats for the URL.

    I hope this clears up the intent of my advice. Thanks for the feedback.

  6. Hi Joel,

    Good call and sorry for the miss on the Apache commands.

    You are correct in the canonical tag application and intent – I think I was a little thrown off by the name of the post and your following explanation of Rewrites with different domains. I see now what you were meaning and on what level you were meaning it, so thanks for the clarification.

    Still a good post 😉

    Cheers.

Comments are closed.