Absolute URL when using Html.CanonicalLink()

Member since: 2006

The Html.CanonicalLink() HTML helper is, well, helpful. :)

However, it (always?) renders a relative URL.

Although both relative and absolute URLs are supported, according to Google absolute URLs are preferred: https://support.google.com/webmasters/answer/139066?hl=en&rd=1

Especially since we might have multiple domains pointing to the same site, for example if edit mode is accessed through a different domain, or if both http and https is supported, etc.

I tried looking at the internals of CanonicalLink(), but the CanonicalLinkRenderer class is internal, and I couldn't see a simple way of making CanonicalLink() render an absolute URL instead.

Of course we can roll our own helper method (and we have), but it just seems like the better choice would be to use the built-in one in Episerver.

So, in short: any ideas on how to make CanonicalLink() render absolute canonical tags?

#147284 Apr 09, 2016 11:01
  • Johan Petersson
    Member since: 2007


    I've had this discussion before... hehe. For me it also makes sense to ALWAYS return an absolute URL. It's actually stripped out if the current request's host is matching the primary host defined in the site definitions. Please see my post here about getting an absolute URL, which you can use in your canonical meta element, and also the comment from Henrik. http://dodavinkeln.se/post/how-to-get-the-external-url-to-content

    #147287 Apr 10, 2016 13:56
    <p>Can we have an option to render the URL as absolute? Google guidelines say it shoud be absolute:&nbsp;https://support.google.com/webmasters/answer/139066?hl=en#2</p> <p></p>
    #148848 May 25, 2016 5:04