Skip to content

Instantly share code, notes, and snippets.

@michaelhjulskov
Last active May 21, 2018 14:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelhjulskov/10a45a77090989ca18b5 to your computer and use it in GitHub Desktop.
Save michaelhjulskov/10a45a77090989ca18b5 to your computer and use it in GitHub Desktop.
Add Canonical tag's to Prestashop
<?php
// Feel free to copy IF you create a href link that point to http://www.dyrefoder.dk from your google indexed website
// Thank you
// Michael Hjulskov
// www.dyrefoder.dk
class FrontController extends FrontControllerCore
{
public function init()
{
parent::init();
$this->context->smarty->assign('canonical_url', $this->getCanonicalUrl());
}
protected function getCanonicalUrl()
{
if (Tools::getValue('ajax'))
return false;
$base_dir = $this->context->smarty->getTemplateVars('base_dir');
$page_name = $this->context->smarty->getTemplateVars('page_name');
if ($page_name == 'index')
return $base_dir;
if ($page_name == 'search' && Tools::getValue('tag'))
return $this->context->link->getPageLink($page_name, true, null, 'tag='.Tools::getValue('tag'));
if ($page_name == 'search' && Tools::getValue('search_query'))
return $base_dir; // TODO - is this the right thing to do? To just set base url as canonical url - or should it be indexed?
//return $this->context->link->getPageLink($page_name, true, null, 'search_query='.Tools::getValue('search_query'));
// List Pages - without any id (relevant might be: n,p,orderby,orderway)
$non_id_list_pages = array('manufacturers','suppliers','best-sales','new-products','prices-drop','all-products');
if (in_array($page_name, $non_id_list_pages))
return $this->context->link->getPageLink($page_name, true, null);
// TODO fix something with the params: n,p,orderby,orderway
//return $this->context->link->getPageLink($page_name, true, null, ((Tools::getValue('p'))?'p='.Tools::getValue('p'):''));
//return $this->context->link->getPageLink($page_name, true, null, ((Tools::getValue('n'))?'n='.Tools::getValue('n'):''));
//return $this->context->link->getPageLink($page_name, true, null, ((Tools::getValue('orderby'))?'orderby='.Tools::getValue('orderby'):''));
//return $this->context->link->getPageLink($page_name, true, null, ((Tools::getValue('orderway'))?'orderway='.Tools::getValue('orderway'):''));
// List Pages - with id parameter (relevant might be: )
$id_detail_pages = array('category','manufacturer','supplier','product','blog','cms');
if (in_array($page_name, $id_detail_pages) && Tools::getValue('id_'.$page_name)){
if ($page_name == 'product')
return $this->context->link->getProductLink(Tools::getValue('id_'.$page_name));
if ($page_name == 'manufacturer')
return $this->context->link->getManufacturerLink(Tools::getValue('id_'.$page_name));
if ($page_name == 'category')
return $this->context->link->getCategoryLink(Tools::getValue('id_'.$page_name));
if ($page_name == 'cms')
return $this->context->link->getCmsLink(Tools::getValue('id_'.$page_name));
if ($page_name == 'supplier')
return $this->context->link->getSupplierLink(Tools::getValue('id_'.$page_name));
return $this->context->link->getPageLink($page_name, true, null, 'id_'.$page_name.'='.Tools::getValue('id_'.$page_name));
}
// Single information pages - without any id (relevant might be: back)
$non_id_single_pages = array(
'contactform','contact','authentication','order','cart','discount','my-account','password',
'order-detail','order-opc','order-slip','history','address','addresses','identity','remindme',
'stores','sitemap'
);
if (in_array($page_name, $non_id_single_pages))
return $this->context->link->getPageLink($page_name, true, null);
// AS4 seo pages - quick fix - TODO improve
if (Tools::getValue('id_seo')) //Tools::getValue('id_search')
return $base_dir . substr(strtok($_SERVER["REQUEST_URI"],'?'), 1);
// other pages - quick fix
// TODO this is meant as temp solution -should be improved
return $base_dir . substr(strtok($_SERVER["REQUEST_URI"],'?'), 1);
// return false;
/* old stuff i might need later
if (strpos($_SERVER['REQUEST_URI'], 'p=') === false){
return $base_dir . preg_replace('/\?(.*)/', '', preg_replace('/\/(.*)/', '', substr($_SERVER['REQUEST_URI'], 1)));
$url = $server_name . strtok($_SERVER["REQUEST_URI"],'?').'?p='.Tools::getValue('p');
*/
}
}
Feel free to copy IF you create a href link that point to http://www.dyrefoder.dk from your google indexed website
Thank you
Michael Hjulskov
www.dyrefoder.dk
insert this in your header.tpl file the right place:
{if isset($canonical_url) && $canonical_url}<link rel="canonical" href="{$canonical_url}" />{/if}
and override your FrontController.php
tada - you should have canonical tags :)
Copy link

ghost commented May 17, 2015

you should use base_dir_ssl to take into account https

@michaelhjulskov
Copy link
Author

Feel free to copy IF you create a href link that point to http://www.dyrefoder.dk from your google indexed website
Thank you
Michael Hjulskov
www.dyrefoder.dk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment