I am currently using a html helper to generate a canonical url but the problem is the server url.

The url is fetched from targetHostName (please correct me if I am wrong)

The problem arrives in the UAT servers where multiple hostNames are defined. The url fetched there is sometimes the one defined in the hostName attribute.


public static string GetAbsoluteUrl(this HtmlHelper helper, Item item)
    var urlOptions = LinkManager.GetDefaultUrlOptions();
    urlOptions.AlwaysIncludeServerUrl = true;
    urlOptions.SiteResolving = true;
    return LinkManager.GetItemUrl(item, urlOptions);

call to Helper:

<link rel="canonical" href="@Html.GetAbsoluteUrl(Sitecore.Context.Item)" />

siteConfig :

    <site name="sitename" patch:after="site[@name='modules_website']" 
          hostName="myhostname1|myhostname2| and more...."

2 Answers 2


If I'm not mistaken, the targetHostName is only used when the context is in another site. When the detected site is within the hostNames of the site, this value is not used and it should use the host from the url.

So to get your canonical right, you might need to try something else. One option is to fetch the url without the server and add the targetHostName yourself.


Please ensure that you have written "targetHostName" always correct in your site.config. If it is not CamelCase (e.g. "targetHostname") it doesn't work. I would also suggest not to use Sitecore.Context.Item in your view but RenderingContext.Current.Rendering.Item or in this case maybe Model.Item if it is the default sitecore Model.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.