mattgadient.com

How to add Google’s canonical tag to Joomla articles

Background:

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=”//www.website.com/current-friendly-url.html”/>{/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.

  • Hi Matt!
    Thanks for this blog, it sent me on the track to find a solution to implement a canonical tag in Joomla. I found one more solution that worked for my needs I wanted to share it here with you and others who might find it useful.

    My problem was I had url:s like mysite.com/page1.htm?var1=one and wanted a canonical tag saying mysite.com/page1.htm that was automatically generated for each page.

    In my template file right after:
    <?php
    defined('_JEXEC') or die('Restricted access');

    I added:
    $canonical = JURI::current();

    And then further down in the template just before the tag, I added:
    <link rel="canonical" href="”>

    Worked like a charm for me. Not sure if it will work in all other cases but give it a try.

    All the best,
    Markus

  • Philip

    Thanks for this plugin. I’ve been searching high and low for a way to add canonical tags to article pages. You are a life saver!

  • Expert SEO India

    Thanks for such a wonderful information, iwas looking for the extension which can install a canonical into my website. My site creating a string after the URLs so i need the plug in. Thanks Again.

  • Thank you Matt!!! Great solution!

  • Loz

    Matt thank you so much, was just what i needed and also thank you for talking it out in such an easy to follow way..Thank you

  • Hi,
    i also had trouble with setting the canonical tag in my joomla pages. I found a simple and quick solution, writing a small script. The script uses the article id to get the seo url an set it to the canonical tag. I also used “$canonical = JURI::current();” and solution like that but if there is a “dirty” link like “/index.php?option=com_content&view=article&id=1&Itemid=109” it will be also set instead of the seo url.

    My script can be easily included in every template from joomla 1.5 to 2.5. The download link and a tutorial can be found here: http://www.concept-br.de/blog/canonical-tag-for-joomla/#more-136

    Hope this helps somebody!

    Best regards, J.

  • thank youuuu
    just a bit correction, from what i saw in jostag documentation, there should be no included..
    eg. when your gave examples like
    {tag}{/tag} , it would be better to write it down like this
    {tag}link rel=”canonical” href=”http://www.website.com/current-friendly-url.html”{/tag}

  • Klement

    You Sir, deserve a medal.

    I have been struggling with this for more than 8 hours straight. I was just about to give up on this.

    THANK YOU!

  • I write a joomla plugin for solving articles duplicate problem. this plugin canonical all article addresses to correct and main address. you can download here:
    http://extensions.joomla.org/extensions/extension/site-management/seo-a-metadata/articles-canonical