1

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.

Helper:

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 :

<sites>
    <site name="sitename" patch:after="site[@name='modules_website']" 
          targetHostName="myhostname" 
          hostName="myhostname1|myhostname2| and more...."
          database="web" 
          virtualFolder="/" 
          physicalFolder="/" 
1

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.

0

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, privacy policy and cookie policy

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