How to add Google’s canonical tag to Joomla articles


I used to use non-SEF url’s on a Joomla site, and recently turned on Joomla’s built-in SEF. Problem is that the old URL’s still “work”. Of course, this is great for people who visit – not so great when GoogleBot comes along.

GoogleBot comes happily along and for any given article sees the original archaic url with a bunch of parameters that have been there forever. Of course, there’s little incentive for it to use the “cleaner” versions. As far as it’s concerned, the archaic stuff is the original, and the new url’s are duplicate content. It could eventually change it’s mind as to the “preferred” page on it’s own, but there are a few fighting factors here. On one hand, the sitemap points to the “new” one, and it’s predominantly linked to through the current site. On the other hand, anyone who’s linked to the site in the past has linked to the “old” one, and let’s face it – it was there first.

To make the change and get Google looking at the right page, we’ll go over the things I tried that didn’t work, and then the thing I tried that did work:

Stuff that didn’t work:

  • .htaccess redirects – I came mighty close with some mod_rewrite rules, but to be honest even if I had eventually got the proper ruleset set-up, copying/pasting the cryptic stuff for every article would have been very time consuming. Of course the plus side had it worked is that the “old” url’s wouldn’t even be accessible anymore, so there wouldn’t be any risk of someone linking to the depreciated url in the future. Really though, the amount of time required to create a custom rewrite rule for each URL is uncanny.
  • Circumventing the existing META tags for each article – I assumed this is a loophole that would have been closed for security reasons, but I gave it a try anyway. The thought being that if you edit one of the existing META’s for an article, you can inject whatever you want into the HEAD section by closing the tag, and then inserting a new one (without closing it). As I suspected, one of the dev’s obviously thought of this, and any quotes you enter are stripped when you save the article.
  • Common SEF plugins – The largest “canonical” plugin I found seemed to be more for redirecting a www to a non-www. I didn’t try it myself, but it really looked like it was designed for large-scale site-wide changes.
  • Template modification – If you look back at a couple articles I wrote back in Feb (2009), you can use PHP to stuff some head tags whereever you want them. Unfortunately this would be even messier to do than setting up .htaccess stuff and would make for a very large PHP file. It also doesn’t make sense to spend the time stuffing canonical into the head section this way when spending the time on .htaccess would give *redirects* which are *better* in this case. A bad solution for many reasons, so I didn’t even bother.

What did work:

  • There is small, relatively unknown plugin for Joomla called “JosTag”. Turns out it’s all that I needed.

You can find it in the Joomla Extensions Directory, or right from the developer’s site (Web Archive). The site is mainly french, but there’s enough english to get you by – the instructions after you install it are dead-easy.

Install it, enable the plugin, and then it’s simply a matter of throwing {tag}whatever-you-want-in-the-head{/tag} in each of your content pages. So for a canonical entry, something like this:

{tag}<link rel=”canonical” href=”//”/>{/tag}

You can stick it anywhere in the content. The downside is that you obviously have to do this for each content page, meaning you’ll have to know the “desired” url. On the plus side, it’s easy to do (especially compared to the above options), it gives you full control for each page, you can stick other stuff in the “head” section if you want it later, and it’s free.