Creating rel="canonical" for pagination pages in OpenCart

In OpenCart, pagination pages are defined through the «page» parameter, even if the SEO URL is enabled in the settings. Therefore, in order to avoid duplicate URLs or similar pages, you should set <link> tag with the attribute «rel ="canonical"» on the pagination pages.

The site already has an article on how to do this on different CMS in PHP.
In this article I want to show how you can make a canonical URL («rel="canonical"») through the standard OCMOD modification mechanism. Of course, no one forbids doing it directly through the code, but it seems to me that through modifications it is more convenient.

We create the modification file rel_canonical.ocmod.xml, which will be located in the /system/ folder. It is in this file that all changes to the engine will be made.

<?xml version="1.0" encoding="utf-8"?>
	<name>rel="canonical" for pagination pages</name>
	<code>rel="canonical" for pagination pages</code>
	<file path="catalog/controller/common/header.php">
			<search><![CDATA[return $this->load->view('common/header', $data);]]></search>
			<add position="before"><![CDATA[
				$data['canonical'] = '';
				if(isset($_REQUEST['page']) && $_REQUEST['page'] > 1){
					$data['title'].= ' | Page '.$_REQUEST['page']; // Add the pagination page number in the browser title, if necessary.
					if((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443){
						$protocol = 'https://';
						$protocol = 'http://';
					$url = $protocol.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
					$urlend = strrpos($url, '?', -1); // Find and delete all parameters (page number, filter parameters, etc.)
					$url = substr($url, 0, $urlend);

					if(mb_substr($url, -1) == '/'){ // Here we leave if the links on the site do not end with /, or delete if they end with /.
						$url = substr($url, 0, -1);

					$data['canonical'] = $url;
	<file path="catalog/view/theme/<your_theme>/template/common/header.tpl">
			<add position="before"><![CDATA[
				<?if($canonical){?><link rel="canonical" href="<?=$canonical;?>"/><?}?>

About the code:
1) In the header controller (catalog/controller/common/header.php) of the site, look for the output of the header template ($this->load->view ('common/header', $data);) and insert our code before rendering it .
2) Create the variable $data['canonical'], into which we add the canonical URL if the pagination page is opened.
3) We check if the «page» parameter is present in the URL, and if so, and it is more than 1, then we execute the code.
4) $data['title'].= '| Page '.$ _ REQUEST['page']; - this line is optional, if you want to add the page number in the browser title.
5) Next, we get the current URL and remove all parameters from it, so that we get a canonical URL.
6) And finally, in the header template of your template, we determine if the $canonical variable is not empty, display the <link rel ="canonical"> tag.


If you have any questions, want to get individual advice or you have an interesting offer, you can contact via the feedback form.

Or write to the email

By submitting the form, you agree to our privacy policy