Skip to content

Instantly share code, notes, and snippets.

@jrruiz
Created April 21, 2015 13:02
Show Gist options
  • Save jrruiz/6e473512ced7cad5b998 to your computer and use it in GitHub Desktop.
Save jrruiz/6e473512ced7cad5b998 to your computer and use it in GitHub Desktop.
Prestashop canonical URL
<?php
class FrontController extends FrontControllerCore
{
public function init()
{
parent::init();
$this->context->smarty->assign('canonical_url', $this->getCanonicalUrl());
}
protected function getCanonicalUrl()
{
$canonical_url = '';
$base_dir = $this->context->smarty->getTemplateVars('base_dir');
$page_name = $this->context->smarty->getTemplateVars('page_name');
$special_pages = array(
'category',
'best-sales',
'cart',
'discount',
'manufacturer',
'new-products',
'prices-drop'
);
if ($page_name == 'index' || $page_name == 'search') {
$canonical_url = $base_dir;
}
elseif (in_array($page_name, $special_pages)) {
$canonical_url = $base_dir . preg_replace('/\?(.*)/', '', preg_replace('/\/(.*)/', '', substr($_SERVER['REQUEST_URI'], 1)));
}
else {
$canonical_url = $base_dir . substr($_SERVER['REQUEST_URI'], 1);
}
return $canonical_url;
}
}
<!-- add this tag inside the <head> tag of your theme's header.tpl -->
<link rel="canonical" href="{$canonical_url}" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment