Created
April 21, 2015 13:02
-
-
Save jrruiz/6e473512ced7cad5b998 to your computer and use it in GitHub Desktop.
Prestashop canonical URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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