Skip to content
● Critical PHP security fixes are out: update PHP on your Grav sites. What to do →

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

Solved by pamtbaau View solution

Started by Tim 4 years ago · 2 replies · 1254 views
4 years ago

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!!! 🙂

4 years ago Solution

@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:
    TWIG
    <link rel="canonical" href="{{ page.url(true, true )}}" />
    

    with:

    TWIG
    <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:
    TWIG
    <link rel="canonical" href="{{ page.url(true, true )}}" />
    

    with:

    TWIG
    <link rel="canonical" 
     href="{{ page.header.routes.canonical ? page.routeCanonical : page.url(true, true )}}" />
    
👍 1
4 years ago

@pamtbaau

Thank you so much! 🙏
Works perfectly 🙂

Suggested topics

Topic Participants Replies Views Activity
Support · by webmasterodp, 2 weeks ago
4 248 2 weeks ago
Support · by nwhitfield, 2 weeks ago
2 213 2 weeks ago
Support · by stell, 4 weeks ago
5 458 3 weeks ago
Support · by gravinator, 2 months ago
5 637 3 weeks ago
Support · by Gravver, 1 month ago
6 430 4 weeks ago