New Shopify Certification now available: Liquid Storefronts for Theme Developers

Add a canonical tag to duplicate products

Frankdigital
Excursionist
13 0 3

Hi there, 

One of my clients is a skincare reseller. 

On their website, they have a big version of specific products and a small version- As it is the same product, they have the same description for the big and mini versions.

Around 20 products have this issue. As an SEO expert, I want to apply a canonical tag for the mini version, telling Google to canonicalise the big version.

Find below an example of two URLS:

 

https://www.website.com/products/name-of-the-product-150ml

https://www.website.com/products/name-of-the-product-30ml

 

How can I do that? Thanks in advance for your support. 

 

Best regards,

 

Francesco

 

 

Replies 5 (5)
PieterB22
Shopify Partner
55 9 15

Hi Francesco,

This is the easiest solution that I could come up with. However it requires that the URL's for the products be identical, except for the 150ml & 30ml option at the end.

{%- if product.handle contains '30ml' -%}

The code above will check the handle of the product to see if it contains the word 30ml. If it does, it will update the url to the following:

<link rel="canonical" href="{{ canonical_url | replace: '30ml', '150ml' }}">

The code will then take the 30ml inside the products url and replace it with 150ml. creating a new canonical url.

{%- else -%}
  <link rel="canonical" href="{{ canonical_url }}">
  {%- endif -%}

Then we add the last bit of logic to tell the site that if the product handle does not contain 30ml, show the default canonical.

 

All you need to do is inside the theme.liquid file replace the:

<link rel="canonical" href="{{ canonical_url }}">

with:

{%- if product.handle contains '30ml' -%}
  <link rel="canonical" href="{{ canonical_url | replace: '30ml', '150ml' }}">
{%- else -%}
  <link rel="canonical" href="{{ canonical_url }}">
{%- endif -%}

Just remember that if the product urls are not identical apart from the 30/150ml at the end, the canonical will point at a non existing product.

Frankdigital
Excursionist
13 0 3

Hi PetrusB22,

Hope you're doing well.

Thanks a lot for sharing the code with me. I'll check and let you know what's happening.

Best Regards,

Francesco

Frankdigital
Excursionist
13 0 3

Hi PetrusB22

 

Sorry for the late reply. I have several instances with not only 30ml but 60ml and the big products can be 150ml or 250ml,

 

How about using this source code:

 

{%- if product.handle contains '30ml' -%}
<link rel="canonical" href="{{ canonical_url | replace: '30ml', '150ml' }}">
<link rel="canonical" href="{{ canonical_url | replace: '30ml', '100ml' }}">
{%- if product.handle contains '60ml' -%}
<link rel="canonical" href="{{ canonical_url | replace: '60ml', '250ml' }}">
{%- if product.handle contains '12ml' -%}
<link rel="canonical" href="{{ canonical_url | replace: '12ml', '150ml' }}">

{%- else -%}
<link rel="canonical" href="{{ canonical_url }}">
{%- endif -%}

 

Please let me know your thoughts. 

 

Best regards, 

 

Francesco

Frankdigital
Excursionist
13 0 3

Hi Petrus,

 

Hope you're doing well. Any thoughts on my previous recommendation?

 

Best regards,

 

Francesco

sandy24
Visitor
1 0 0

click the link and get the free voucher: https://www.google.com

You will be redirected to the new address in five seconds.

If you see this message for more than 5 seconds, please click on the link above!