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.
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}' -%}
{% elsif template contains 'blog' and current_tags %}
{%- assign new_canonical_url = blog.url | prepend: '{Domain}' -%}
{%- else -%}
{%- endif -%}
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
- This ensures the canonical tag always points to the main collection page, no matter which filters or tags are applied.
- 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.
- 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 tag. It should now point to the main collection page without any tags or filters.