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="/"