Remove page=2 from canonical links for Opencart 3

The developers decided that in Opencart 3+ for SEO it would be very useful to add a link to the current page as canonical, even if it is a pagination page. This is an awful SEO solution, so you need to make some changes.

So, you need:
from <link href=”https://domain/path?page=2 ” rel=”canonical” /> delete ?page=2,
from <link href=”https://domain/path?page=3 ” rel=”canonical” /> delete ?page=3 etc.

I didn’t find any articles about this problem in Google, but there is a solution. Edit the
file/catalog/controller/product/category.php, look for a string 338 (the line number may be different) and instead of this:

if ($page == 1) {
	$this->document->addLink($this->url->link('product/category', 'path=' . $category_info['category_id']), 'canonical');
} else {
	$this->document->addLink($this->url->link('product/category', 'path=' . $category_info['category_id'] . '&page=' . $page), 'canonical');
}

write this:

if ($page == 1) {
	$this->document->addLink($this->url->link('product/category', 'path=' . $category_info['category_id']), 'canonical');
} else {
	$this->document->addLink($this->url->link('product/category', 'path=' . $category_info['category_id']), 'canonical');
	$this->document->addLink($this->url->link('product/category', 'path=' . $category_info['category_id'] . '&page='. $page), '');
}

After that, edit /catalog/view/theme/yourtemplate/template/common/header.twig and instead of this:

{% for link in links %}
<link href="{{ link.href }}" rel="{{ link.rel }}" />
{% endfor %}

write this:

{% for link in links %}
{% if link.rel %}

{% else %}
{% endif %}
{% endfor %}

Don’t forget to clean the cache in the admin panel.

About iryna

I'm Iryna, a web developer from Ukraine with a decade of experience solving complex technical challenges in the world of freelance. Throughout my career, I've worked on everything from troubleshooting server-side issues and optimizing website performance to enhancing user interfaces. On this blog, I share detailed solutions to the technical problems I’ve encountered and methods that have worked best for me. In addition to my technical expertise, I’m also passionate about digital drawing. I hope the tutorials and insights I provide here will help both fellow developers and creatives alike in their own projects.

Check Also

How to remove slash in Opencart category urls

Opencart version 2.3, the task was to make all URLs like /category/subcategory/product and remove links …

Leave a Reply

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