I’ve created a new OpenCart 1.5.6 store – http://www.example.eu - however, a lot of its pages are identical (in terms of text context) to pages in an old store which is still running – http://www.example.co.uk
With both sites indexed, I’d like to reference all identical pages at the old site by including canonical tags in the new site:
e.g. The “About Us” page at http://www.example.eu/about-us should ideally have something like that in its head section:
<link rel="canonical" href="http://www.example.co.uk/delivery"/>
e.g. The “Delivery” page at http://www.example.eu/delivery should ideally have something like that in its head section:
<link rel="canonical" href="http://www.example.co.uk/delivery"/>
I’m aware how that can be achieved in theory, but cannot apply it in practice. Additionally, while I’m mostly concerned about all the Information-type pages, ideally I’d love to be able to specify the exact pages that will have the canonical tag, something like:
If (this page is About-Us OR Delivery OR … OR … OR … OR …)
{
Include Canonical Tag of the type <link rel="canonical" href="http://www.example.co.uk/xxxxxxxx"/> ,
where xxxxxxx changes accordingly, depending on the page.
}
Else
{
Don’t include Canonical Tag
}
The best way to achieve this is to modify your Document class, set your canonical in your controller, then render it in the header controller.
Edit system/library/document.php
add:
private $canonical;
to your class variables up top.
Then add the following methods:
public function setCanonical($url) {
$this->canonical = $url;
}
public function getCanonical() {
return $this->canonical;
}
In your header controller add:
$this->data['canonical'] = $this->document->getCanonical();
Then you can set your canonical dynamically in the header controller:
if (isset($this->request->get['_route_'])):
$canonical_route = $this->request->get['_route_'];
else:
$canonical_route = '';
endif;
$this->document->setCanonical($this->data['base'] . $canonical_route);
Or you can set it manually in your controllers using the setCanonical method.