Mar 5 2009

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.”


May 7 2006

A New Hope

It’s been a long time, but I’ve started fiddling a bit with web design again. While I do like K2, I prefer to make my own design. And K2 was always meant to be temporary here until I found my long lost design groove again. And I think I spotted it out in the gutter, drunk out of its mind. It’s in rehab now.

I’ve started work on a WordPress theme tentatively called Kestrel. I’m writing the basic layout now with the theme development page readily available in another tab.

I do admit that it feels a bit messy to use pure PHP instead of a template language like Movable Type uses, but I’ll manage.

Since doing live editing gets messy I’ve set up a local web server hosted on my file server. lighttpd as web server, PHP and MySQL.

lighttpd is very nice for local development — it doesn’t include the entire kitchen sink like Apache does. It took a bit of work to get it going the way I wanted, though. For a while it decided to send me the PHP source code instead of passing it on to PHP – but only when I accessed a PHP file from my workstation. When viewed locally it worked properly.


Feb 5 2003

Like, switch!

Yup. Took me all of fifteen minutes to make a quick PHP hack that points you to different style sheets. It’s in the bottom part of the sidebar. Currently only two styles available, but I’ll make more, you can count on that… I’m already getting tired of Warranty Void, the latest one.

I always pick strange names for my style sheets. The next one will be called Nuisance Value.