Canonical URLs in WordPress
Like any good obsessive-compulsive blogger, I frequently pour over my web site statistics looking for interesting stuff. One thing that caught my eye is that search results coming in from Google tends to link to threaded comment pages for entries.
For example, I’d get hits to /blog/2009/03/05/entry-slug/comment-page-1/, which is an URL that’s pretty hard to actually find on the page — it’s only used for permanent links to entry comments, with an anchor to the comment ID tacked on, like this.
What I’d prefer to see is incoming hits to the actual blog entry URL – /blog/2009/03/05/entry-slug/ in this case. Luckily, there’s a smart and simple solution — canonical URLs. The Google Webmaster blog explains it nicely with examples.
So I simply add a canonical tag referencing the preferred URL in each page. Now, it’s just the preferred URL on my side — this is just a hint to search engines that tells them that this page is a duplicate, and that I’d prefer them to let results point to one particular page of these duplicates. In 99% of the cases it will be respected, though.
The actual code is just a few lines. Add this in header.php of your theme, somewhere in the head:
<?php if (is_singular()) { ?>
<link rel="canonical" href="<?php the_permalink() ?>" />
<?php } ?>
is_singular is a function that returns true if any of is_single(), is_page() or is_attachment return true — in other words, if you’re currently visiting a single entry, a page or an attachment.
With this, each entry sub-page of comments has the main entry URL as their canonical URL. Now to wait a few days and see when the search engines1 pick up the change.
1 And by “search engines” I mean “Google.”