Adding canonical URL’s to OpenCart

I wanted to address duplicate content on a project by adding canonical URL’s┬ábecause the product page (e.g.: mydomain.com/category/category.html) shares content on some category pages of the same name (e.g.: mydomain.com/category).

OpenCart does not allow canonical URL’s by default and although you can buy modules to address this I decided to code my own solution.

To do this I put the following code in the header.tpl file of my OpenCart theme:

if (strpos($link['href'], 'my-page.html') !== FALSE)
 echo('<link href="http://mydomain.com/my-page/" rel="canonical" />');

In other words, if the URL matches the product page name “my-page.html” then it will point to the canonical page which is for example “mydomain.com/my-page”.

Leave a Comment

Your email address will not be published. Required fields are marked *