Currently an "Alias" in Sitecore will produce multiple routes to the same content item which can negatively affect SEO in some cases.
I am looking for a way to programatically check whether the current Page/Item/URL/Request is using an alias or not.
I was hoping there would be something along the lines of:
Sitecore.Web.WebUtil.IsAlias
Any ideas on how to check for aliases?
-------UPDATE-------
Here is my current solution which appears to work just fine... Unless anyone has a better ideas?:
protected bool IsAlias
{
get
{
string fullPath = LinkManager.GetItemUrl(Sitecore.Context.Item);
return !HttpContext.Current.Request.RawUrl.StartsWith(fullPath, StringComparison.OrdinalIgnoreCase);
}
}
------ UPDATE 2 ------
Here is a working solution based on Yan's suggestions. I don't believe Sitecore.Context.Database.Aliases.GetTargetUrl() is working as of Sitecore 6.4.1. so I had to improvise a little.
if (Sitecore.Configuration.Settings.AliasesActive &&
Sitecore.Context.Database.Aliases.Exists(HttpContext.Current.Request.RawUrl))
{
const string format = "<link rel=\"canonical\" href=\"{0}://{1}{2}\"/>";
Item targetItem = Sitecore.Context.Database.GetItem(Sitecore.Context.Database.Aliases.GetTargetID(HttpContext.Current.Request.RawUrl));
return String.Format(format, HttpProtocol, HttpContext.Current.Request.Url.Host, LinkManager.GetItemUrl(targetItem));
}