Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

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

Solved by pamtbaau View solution

Started by Tim 4 years ago · 2 replies · 763 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 Thomas, 1 week ago
2 54 12 hours ago
Support · by Anna, 3 days ago
2 60 15 hours ago
Support · by Justin Young, 16 hours ago
1 30 15 hours ago
Support · by Duc , 1 week ago
2 65 5 days ago
Support · by Colin Hume, 1 week ago
2 57 5 days ago