How do I fix canonical tags that generate double domain names?

Topic summary

A user discovered that canonical tags across all pages are generating malformed URLs with the domain name appearing twice. They provided a screenshot showing the homepage’s canonical tag displaying this duplication.

The issue appears to originate in the theme.liquid file. The user shared the relevant code snippet but noted they cannot understand how to fix it.

Current Status:

  • Another user responded with a proposed code modification to fix the double domain issue
  • The suggested solution involves modifying how the canonical URL is constructed in theme.liquid
  • The code snippets provided appear corrupted or reversed (possibly encoding issues), making the exact fix difficult to verify

Key Technical Detail:
The problem involves Liquid template code that splits and reconstructs canonical URLs, likely incorrectly concatenating the shop URL with existing URL components.

The discussion remains open, awaiting confirmation whether the proposed solution resolves the canonical tag duplication.

Summarized with AI on November 19. AI used: claude-sonnet-4-5-20250929.

Issue: There may be an issue with how the canonical tags are being generated for all pages. It appears that the canonical tag is mistakenly pointing to a URL that includes the domain name twice (screenshot below of the homepage’s canonical).

I located this in the theme.liquid but i can’t make sense of it. What do i need to do to correct so that my canonical url doesn’t list my domain 2x?

{%- if collection.url -%}

{%- assign canonicalurl = canonical_url | split: ‘/’ -%}

{%- assign updated_canonical = shop.url | append: ‘/’ | append: canonicalurl[3] | append: ‘/’ -%}

{%- assign vendor = canonicalurl[4] | split: ‘vendor’ -%}

{%- assign vendor_value = vendor[1] | split: ‘=’ -%}

{%- if vendor[1] -%}

{%- assign updated_canonical = updated_canonical | append: vendor_value[1] -%}

{%- else -%}

{%- assign updated_canonical = updated_canonical | append: canonicalurl[4] -%}

{% endif %}

{%- else -%}

{%- endif -%}

Hello @servicedesk_2 ,

The code you provided is responsible for generating the canonical URL in your theme.liquid file. It appears to be extracting parts of the URL and reconstructing it in a specific format. To fix the issue with the double domain name in the canonical URL, you can modify the code as follows:

{%- if collection.url -%}
  {%- assign canonicalurl = canonical_url | split: '/' -%}
  {%- assign updated_canonical = shop.url | append: '/' | append: canonicalurl[3] -%}

  {%- if canonicalurl.size > 4 -%}
    {%- assign updated_canonical = updated_canonical | append: '/' | append: canonicalurl[4] -%}
  {%- endif -%}

  
{%- else -%}
  
{%- endif -%}

Hope this can help. Let us know if you need any further support.

Ali Reviews team.