Jump to content

How to set up canonical URL for homepage?


Tomi14

Recommended Posts

 How to set up canonical URL for homepage?
 

I have missing canonical URL for few pages on my site (8 pages from ~100). But one of them is homepage:(

If I check source it look:

script type= text/javascript"

...date_format_full":"Y-m-d H:i:s","id":2},"page":{"title":"","canonical":null,"meta":{"title":"Domofony Vidos - Sklep z wideodomofonami VIDOS","description...

How to fix this part: canonical":null?

 

Link: Vidos Domofony

Edited by Tomi14 (see edit history)
Link to comment
Share on other sites

  • 1 month later...

I added this line to header.tpl. I think it should work only for main page, but i don't have time to check it. Maybe there is a better solution, but I don't know how

 

Quote

 

{include file="_partials/header-layout/header_type_1.tpl" }
<meta name="robots" content="index,follow">
<meta name="author" content="Vidos-Domofony">

<link rel="canonical" href="https://vidos-domofony.pl/">

 

 

Edited by Tomi14 (see edit history)
Link to comment
Share on other sites

  • 3 months later...
  • 2 months later...

I had the same issue in PrestaShop 8.1.1 and made this solution:

In the file: /themes/XXXX/templates/_partials/head.tpl 

I changed this (lines 42-44):

  {if $page.canonical}
    <link rel="canonical" href="{$page.canonical}">
  {/if} 

To this:

  {if $page.canonical}
    <link rel="canonical" href="{$page.canonical}">
  {elseif $page.page_name == 'index'}
    <link rel="canonical" href="https://{$smarty.server.HTTP_HOST}/">
  {/if} 

In additon to elisolution's answer above, this also checks whether we are actually on the home page (not just whether the canonical was set or not).

I tested this solution a bit and as far as I can see, it works as expected. Otherwise, please let me know!

Thanks :)

PS: Remember to change "https" to "http" if your site isn't handling requests through the HTTPS protocol. You could fiddle with variables like $smarty.server.REQUEST_SCHEME or $smarty.server.HTTPS to check automatically, but it seemed like overkill to me, as I always use the HTTPS protocol on my websites these days.

Edited by LarsM (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...
On 6/5/2023 at 6:33 PM, elisolution said:

I added in 

/public_html/themes/XXXX/templates/_partials/head.tpl 

lines 42-43:

{if $page.canonical = 'null'}
    <link rel="canonical" href="https://mysite.com">
  {/if}
 

 

It work vey well

I changed it in this way because It works better 👇:

{if $page.canonical}
  <link rel="canonical" href="{$page.canonical}">
{elseif $page.canonical = 'index'}
  <link rel="canonical" href="https://mysite.com">
{/if}

 

 

Link to comment
Share on other sites

  • 1 month later...

to implement the canonical for multilanguage use this code:

 

  {if $page.canonical}
    <link rel="canonical" href="{$page.canonical}">
  {elseif $page.page_name == 'index'}
    <link rel="canonical" href="{$smarty.server.SCRIPT_URI}">
  {/if}

 

  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...

 

On 9/17/2023 at 3:15 PM, elisolution said:

I changed it in this way because It works better 👇:

{if $page.canonical}
  <link rel="canonical" href="{$page.canonical}">
{elseif $page.canonical = 'index'}
  <link rel="canonical" href="https://vidos-domofony.pl">
{/if}

 

 

 

With this, now I have 14 pages with Canonicalised errors.

Do you have any idea how to fix it?  Most of this pages are sites, from help category: https://vidos-domofony.pl/category/9-pomoc

With homepage your solutions works great

image.thumb.png.0e14819e3a6c00068c5b8fb42f61a4e7.png

  

On 8/28/2023 at 3:19 PM, LarsM said:

I had the same issue in PrestaShop 8.1.1 and made this solution:

In the file: /themes/XXXX/templates/_partials/head.tpl 

I changed this (lines 42-44):

  {if $page.canonical}
    <link rel="canonical" href="{$page.canonical}">
  {/if} 

To this:

  {if $page.canonical}
    <link rel="canonical" href="{$page.canonical}">
  {elseif $page.page_name == 'index'}
    <link rel="canonical" href="https://{$smarty.server.HTTP_HOST}/">
  {/if} 

In additon to elisolution's answer above, this also checks whether we are actually on the home page (not just whether the canonical was set or not).

I tested this solution a bit and as far as I can see, it works as expected. Otherwise, please let me know!

Thanks :)

PS: Remember to change "https" to "http" if your site isn't handling requests through the HTTPS protocol. You could fiddle with variables like $smarty.server.REQUEST_SCHEME or $smarty.server.HTTPS to check automatically, but it seemed like overkill to me, as I always use the HTTPS protocol on my websites these days.

 

But with this, I have only 7 Canonicals missing

image.thumb.png.f4a1fd39c0f540396ba6f84430bef576.png

 

Edited by Tomi14 (see edit history)
Link to comment
Share on other sites

  • 2 months later...

Hi!

If you want have canonical in all pages use thtis varible for current web address (the url of the page viewing) in head.php

{$urls.current_url}

so:

{block name='head_seo_canonical'}
  {if $page.canonical}
    <link rel="canonical" href="{$page.canonical}">
  {else}
    <link rel="canonical" href="{$urls.current_url}">
  {/if}
{/block}

But remember that canonical url need only for page that have possible duplicated.
index, contact, cms pages, new product and other list pages, can never possible have duplicated, becouse are like static pages, not like catalog pages.

Bye

Link to comment
Share on other sites

  • 3 months later...

After some researching and testing, my current solution in my `head.tpl` looks like this: 

    {block name='head_seo_canonical'}
        {if $page.canonical != ''}
            <link rel="canonical" href="{$page.canonical}">
        {elseif $smarty.server.SCRIPT_URI != ''}
            <link rel="canonical" href="{$smarty.server.SCRIPT_URI}">
        {elseif $smarty.server.REDIRECT_URL != ''}
            <link rel="canonical" href="{$smarty.server.REDIRECT_URL}">
        {else}
            <link rel="canonical" href="{$urls.current_url}">
        {/if}
    {/block}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...