How can I set a canonical link to an external URL?

Hi,

how can I set a canonical link to an external URL?

Example:
I want to publish that is already published on another blog. To avoid duplicate content I should set the canonical link to the original article URL.

To manipulate the canonical link I have an option under

Advanced > Route Overrides > Canonical Route

But the result is not what I need.

When I put “https://original-blog.com/original-url” into the field the result is

<link rel="canonical" href="https://new-blog.com/https://original-blog.com/original-url" />

What is the right way to change the canonical link to an external URL?

Thanks!!! :slight_smile:

@amihoro, It appears this issue has already been raised before, but maybe too easily marked as being solved…

Currently, when using routes.canonical together with {{ page.url(true, true) }} as used in templates, the route.canonical value will be appended to the base route.

When using {{ page.routeCanonical }} as suggested in the issue, the generated canonical will be the value of routes.canonical, but if not set, it will not be in line with the recommendation by Google to use absolute urls.

Alternative workarounds:

  • Create an inherited theme and use the suggestion from the issue. In your inheriting theme, in template base.html.twig, replace:
    <link rel="canonical" href="{{ page.url(true, true )}}" />
    
    with:
    <link rel="canonical" href="{{ page.routeCanonical }}" />
    
  • If you want an absolute Url as recommended by Google, create an inherited theme and in template base.html.twig, replace:
    <link rel="canonical" href="{{ page.url(true, true )}}" />
    
    with:
    <link rel="canonical" 
       href="{{ page.header.routes.canonical ? page.routeCanonical : page.url(true, true )}}" />
    
1 Like

@pamtbaau

Thank you so much! :pray:
Works perfectly :slight_smile: