We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Canonical URL on Filtered Collection

Canonical URL on Filtered Collection

Shamilton44
New Member
6 0 0

Hoping that community can help. I am looking to create some links to a filtered collection page using tags.

 

So I have a collection URL like

/collections/collection-name/product-tag

Right now my canonical is showing exactly the above although I would prefer to canonicalize to the main page and not include the tag. Is there a way to do this?

 

My current canonical liquid tag is splitting pagination as well and unsure on syntax to strip the filter, if I even can.

 

<link rel="canonical" href="{{ canonical_url | split: '?' | first}}">

 

Replies 2 (2)

Shamilton44
New Member
6 0 0

Dropping a solution in that seems to be working.

 

{%- assign new_canonical_url = canonical_url -%}
{%- if new_canonical_url contains '?' -%}
    {%- assign url_params = new_canonical_url | split: '?' | last -%}
    {%- assign new_canonical_url = new_canonical_url | split: '?' | first -%}
{%- endif -%}
  
{% if template contains 'collection' and current_tags %}
    {%- assign new_canonical_url = collection.url | prepend: '{Domain}' -%}
    <link rel="canonical" href="{{ new_canonical_url  | split: '?' | first }}">
{% elsif template contains 'blog' and current_tags %}
    {%- assign new_canonical_url = blog.url | prepend: '{Domain}' -%}
    <link rel="canonical" href="{{ new_canonical_url  | split: '?' | first }}">
{%- else -%}
    <link rel="canonical" href="{{ new_canonical_url | split: '?' | first }}">
{%- endif -%}

Avitanshi_17
Pathfinder
100 10 14

If you want your filtered collection pages (like /collections/collection-name/product-tag) to point to the main collection page (e.g., /collections/collection-name) as the canonical URL, you can adjust your theme code to do this. 

Here's how:

Steps to Fix Canonical URLs for Filtered Pages:

Update the Canonical Tag Code: In your Shopify theme, go to the theme.liquid or your collection template file. Look for the part where the canonical URL is defined (usually <link rel="canonical" ...>). Replace it with this code:
liquid
Copy code
<link rel="canonical" href="{{ collection.url | within: collection }}" />

  1. This ensures the canonical tag always points to the main collection page, no matter which filters or tags are applied.
  2. Fix Pagination Issues: If pagination (like ?page=2) is being added to the URL, this code will still work. Shopify automatically strips the ?page=2 part when generating the canonical URL using collection.url.
  3. Check Your Changes:
    • Go to a filtered collection page on your store.
    • Right-click on the page and select Inspect or View Page Source.
    • Look for the <link rel="canonical"> tag. It should now point to the main collection page without any tags or filters.
Stay inspired,
Best regards,
Avitanshi
Contact Us at www.mastroke.com