How should I implement canonical urls in my prestashop theme?

- 1 answer

Ad

canonical urls are currently activated on the shop, but the won't be written in frontend source code. So I took a look even to the default-bootsrap theme, the last from github, but it seems it doesn't implement any canonical url.

I also checked all the controllers, but it seems no one sets a $canonical_url (something like..) smarty var, so what the hell is the related backoffice option?

I searched the web but found nothing really useful.

Ad

Answer

Ad

The backoffice option Canonical url is only used in controllers.

If you have activated the url rewriting option and try to access this link:

http://dev.test.com/index.php?id_product=1&controller=product

You will be redirected to, for example:

http://dev.test.com/tshirts/1-T-shirts-a-manches-courtes-delaves.html

Here is an extract from the canonicalRedirection method from FrontController class:

/**
 * Redirects to canonical URL
 *
 * @param string $canonical_url
 */
protected function canonicalRedirection($canonical_url = '')
{
    [...]

    $redirect_type = Configuration::get('PS_CANONICAL_REDIRECT') == 2 ? '301' : '302';
    header('HTTP/1.0 '.$redirect_type.' Moved');
    header('Cache-Control: no-cache');
    Tools::redirectLink($final_url);
}

In this method we get the canonical redirect option you selected in the BackOffice with this line Configuration::get('PS_CANONICAL_REDIRECT').

If you want canonical url in your html header you'll have to write or get a module for that as it is not included in Prestashop.

Ad
source: stackoverflow.com
Ad