Ben's Pro Tips

Tech Tips Made Easy

February 6, 2014 eCommerce, PHP Tips canonical, opencart

Adding canonical URLs to your OpenCart pages (for free)

OpenCart is a pretty good free PHP shopping cart system. Despite its popularity, some of its missing features are glaring, especially when it comes to SEO. OpenCart does implement the rel=”canonical” tag on its product pages. However, there’s no canonical tag on any of the category pages or the all important homepage. If you install OpenCart without fixing this problem, a Google site:yourdomain.com search will show a million variations of each category diluting the results, “link juice”, and earning relegation to the “See this search with duplicate pages”.

A quick search online found a number of SEO plugins that fix this, however most of them are in the $15 – $75 range. Not one to drop $75 on a single meta tag, I dug into the source looking for a solution. Fortunately, I found a relatively easy one.

1) Edit your catalog/controller/product/category.php file
2) Find this line:

if ($category_info) {

3) Immediately after it, add the following lines:

$can_url=$this->url->link("product/category","path=".$this->request->get['path']);
$this->document->addLink($can_url,"canonical");

4) Now edit catalog/controllers/common/home.php
5) Find this line:

$this->document->setDescription($this->config->get('config_meta_description'));

6) Immediately after it add:

$can_url=$this->url->link('common/home');
$can_url=str_replace('index.php?route=common/home', '', $can_url); 
$this->document->addLink($can_url,'canonical');

7) Presto! You have canonical urls!

If you don’t want to edit any source files and would rather have a convenient vqmod plugin version of this update, you can buy it on CodeCanyon by clicking here. It’s just $5 and also includes canonical URLs on your info pages and a fix for the strange default behavior of OpenCart product canonical urls. Update: buildmyecommerce.com put together a demo video, you can see it here.

29 to “Adding canonical URLs to your OpenCart pages (for free)”

Trackbacks/Pingbacks

  1. Manish says...

    U r great dude,
    100 % worked. i was looking for this code for more then 1 months. Thanks a lot.

  2. alex.i.am says...

    Great, Simple, Concise information; I have added the above codes in a vqmod file it works superb.
    Thanx for you efforts ;o)

  3. Jason says...

    Just purchased your vqmod from CodeCanyon for http://www.lubeclean.ca. Works great and for the price I couldn’t be bothered to write my own vqmod to do the same thing. Thanks!

  4. bern says...

    Hi Ben,
    Glad I found your site! Just to clarify, in step 1, the category.php you are referring to is in the product directory as opposed to the module directory?
    Thanks for your help,
    Bern

    • ben says...

      Yes it is, I’d left it a bit ambiguous, I fixed the text to make it more clear.

      Thanks,
      Ben

  5. Mark says...

    I have a slight problem with this code. My whole site is served over SSL and when insert this code the canonical directs to the HTTP version instead of HTTPS.

    Any ideas on how to correct this?

    Thanks!

    • ben says...

      Hi Mark,

      Try editing /config.php and changing the define HTTP_SERVER to the https:// URL of your site. This should correct the problem you’re having.

      -Ben

      • Mark says...

        Thanks for the reply Ben.

        I had already done this when I installed my SSL certificate.

        I can get the home page canonical to point to the HTTPS page by inserting this:

        $this->document->addLink(HTTPS_SERVER, ‘canonical’);

        But the same does not work for the category pages 🙁

        • ashish says...

          Dear Mark,

          I am having the same problem. I install ssl certificate on my website which working on admin area but not on store front due to canonical urls in home, categories and information. Where have you inserted the above mentioned code? Please advise and help on this

          • Danny says...

            You could just replace the following lines (which you just inserted):
            $this->document->addLink($can_url,”canonical”);

            with:

            $this->document->addLink(str_replace(‘http://’, ‘https://’, $can_url),”canonical”);

            I’m not sure this will work, and definitively isn’t the nicest solution but I’d guess it’s worth the try?

  6. Digital Marketing Agency says...

    Just input this into the code and it has worked a dream. I had tried SEO Mega Pack and for most things it works fine, but for the canonical we were experiencing issues. Thanks very much for this info. Chris

  7. johnhgaspay says...

    I’ve check to see if this is working, and Yes It’s working. I’ll subscribe to your site to see future posts.o see future posts.

  8. pravn thakur says...

    can we use this code for opencart 2.0 <

  9. steve says...

    Does this work opencart 2. Very much needed. Please share your experience with oc 2. Thanks

    • ben says...

      Hi Steve,

      I’ve updated my canonical plugin to work with OpenCart 2.0, when I get a chance I’ll update this page as well.

      -Ben

  10. alan says...

    im having some trouble with my category ,
    store/category/subcategory
    store/subcategory
    is going to the same place
    i tried to do this canonical , but didnt work =/ , can somebody help me ?

    • ben says...

      Hi alan,

      Yes, that is one of the SEO problems of OpenCart, many duplicate URLs which dilute your ranking. I created this plugin which will give consistent links to each product across your site and 301 redirect all the duplicate URLs to the proper URL.

      -Ben

  11. Web Design Company says...

    This is a great fix and has really helped me with some tricky issues. We attempted to use SEO Mega Pack but that did not work, so this was a great piece of information. Thanks. Chris

  12. Jeroen says...

    Hi Ben,

    Thanks for this post! Works great!! I’m wondering if you could add a canonical url solution for the manufacturer pages.

    Thanks!

    Best regards,
    Jeroen

  13. Jason says...

    Just what I needed. Thank you very much for making the process easy and the code. Cheers!

  14. Vivek Kathiriya says...

    Thanks for your Pro Tips and helping Users. I have used for http://lastamp.gr/ and working perfect.

  15. Rick says...

    Hey, I’m getting 2 canonical tags. One is correct and another is a wrong one. What are the places from where canonical tag comes other than category.php and home.php ?

  16. Hasan Junaid Hashmi says...

    Hi
    I am working with OC 2.3.0.2
    I added SetCanonical and GetCanonical functions in docuemnt file. Then I ued SetCanonical only for manufacture_info function of my manufacturer controller in catalog folder of my theme. Then I added if isset($canonical) in header tpl and and code to display canonical using get canonical function
    It never appears

  17. Web Design Sydney says...

    Hi, thank you for sharing info with community is this for Opencart 1.5 or 2.0?

  18. Sumit Malviya says...

    How can we add canonical without trailing slash which can reflect to the page with slash..

  19. Jonnie Zwartjes says...

    This solution works great on an opencart 2.0 stre i have, but it does not work on my 1.5.4 store. Is this a known issue?

Leave a comment

Time limit is exhausted. Please reload the CAPTCHA.