Friday , December 20 2024

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 admin

Hi there! My name is Iryna, I am a web developer from Ukraine. While working on freelance projects I often face different technical issues and in this blog I publish articles on how to solve them. I am also interested in digital drawing and in this blog you will find brief tutorials that helped me and hopefully can help you as well.

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 *