Canonical URL liquid

Solved
Gonkas
New Member
3 0 1

Hi,

We are trying to return the true canonical value instead of an url with a search parameter.

So instead of having something like this:

[domain]/collections/jackets-coats?page=srnqqdjnmld

We would like to show something like this:

[domain]/collections/jackets-coats

Does anyone knows why the following code returns an empty string?

 

 {% assign canonicalUrl = {{ canonical_url }} %} 
 
 {% assign secondUrl = canonicalUrl | split: '?' | last %}
 {% if secondUrl != 'page=2' %}
 {% assign canonicalUrl = canonicalUrl | split: '?' | first %}
 {% endif %}

<link rel="canonical" href="{{ canonicalUrl }}" />

 

 Code above works in a liquid sandbox.

Thanks.

Cheers,

Gonkas

Accepted Solutions (2)

Accepted Solutions
LitCommerce
Trailblazer
254 61 67

This is an accepted solution.

Hi @Gonkas,

Please change code:

{% assign canonicalUrl = canonical_url %} 
 
{% assign secondUrl = canonicalUrl | split: '?' | last %}
{% if secondUrl != 'page=2' %}
{% assign canonicalUrl = canonicalUrl | split: '?' | first %}
{% endif %}

<link rel="canonical" href="{{ canonicalUrl }}" />

Hope it helps!

LitCommerce - The Most Simple & Affordable Multi-channel Selling Tool.
Effortlessly sell on biggest marketplaces like Amazon, Etsy, eBay, Facebook etc with bulk listing tool, real-time sync & smart order management. Use LitCommerce free for 1-year now!

View solution in original post

isis-mtnez
Tourist
22 3 3

This is an accepted solution.

The issue is extra {{  }}      {% assign canonicalUrl = {{ canonical_url }} %}

This is the correct code:   {% assign canonicalUrl =  canonical_url  %}

But the canonical URL is the page's "default" URL with any URL parameters removed so nothing more needs to be done.

For example, for a product in a collection with a variant selected:

https://example.com/collections/classics/products/shoes?variant=17287731270

The canonical URL is the product page:  https://example.com/products/shoes

Have a great day!!!


- Was I helpful? Like to let me know

- Did I answer your question? Mark my reply as Accepted Solution

 

Give me a Like if I helped you
Please mark as Accepted Solution if I fixed your problem!
Thank you :)

View solution in original post

Replies 3 (3)
LitCommerce
Trailblazer
254 61 67

This is an accepted solution.

Hi @Gonkas,

Please change code:

{% assign canonicalUrl = canonical_url %} 
 
{% assign secondUrl = canonicalUrl | split: '?' | last %}
{% if secondUrl != 'page=2' %}
{% assign canonicalUrl = canonicalUrl | split: '?' | first %}
{% endif %}

<link rel="canonical" href="{{ canonicalUrl }}" />

Hope it helps!

LitCommerce - The Most Simple & Affordable Multi-channel Selling Tool.
Effortlessly sell on biggest marketplaces like Amazon, Etsy, eBay, Facebook etc with bulk listing tool, real-time sync & smart order management. Use LitCommerce free for 1-year now!

View solution in original post

isis-mtnez
Tourist
22 3 3

This is an accepted solution.

The issue is extra {{  }}      {% assign canonicalUrl = {{ canonical_url }} %}

This is the correct code:   {% assign canonicalUrl =  canonical_url  %}

But the canonical URL is the page's "default" URL with any URL parameters removed so nothing more needs to be done.

For example, for a product in a collection with a variant selected:

https://example.com/collections/classics/products/shoes?variant=17287731270

The canonical URL is the product page:  https://example.com/products/shoes

Have a great day!!!


- Was I helpful? Like to let me know

- Did I answer your question? Mark my reply as Accepted Solution

 

Give me a Like if I helped you
Please mark as Accepted Solution if I fixed your problem!
Thank you :)

View solution in original post

Gonkas
New Member
3 0 1

Thanks both, it was extra {{ }}.

Shopify should work that out but it doesn't so needs this "hack".

 

Have a great day!