4

I have inherited a Sitecore architecture that is a mess from an SEO duplicate content POV.

There are multiple aliases that have been created (and indexed by the search engines) for many of the 2nd tier pages of the site.

Due to server issues, I am not able to 301 redirect these duped pages, so I would like to use the rel=canonical tag in an attempt to try and get Google/Bing to recognize the correct pages I would like to appear in the index.

I have blocked the most extraneous duped pages with a robots.txt file, however, since Google/Bing have already spidered many of the duped pages, I need to keep them accessible to the spiders, BUT removed from the index.

The catch is, since the duped pages are aliases (and don't really physically exist in Sitecore that I can find), I am not sure how to go about using rel=canonical - or if I even can in this situation..?

2
  • 2
    This probably belongs over on webmasters or superusers, unless you have a programming related question in here.
    – Mech Software
    Nov 16, 2011 at 16:30
  • 2
    Not sure I agree with the move... it is a code related solution. Nov 17, 2011 at 14:51

3 Answers 3

4

Mike, I think the easiest, and first place, I would start is to modify your Layout (.aspx) such that pages using an alias have the <link rel="canonical" href="..."/> in the head.

You can leverage code provided with this answer to help you.

Essentially, add a asp:Literal to the <head> of your .aspx page. Now, on Page_Load of your .aspx.cs you can do the following

if (global::Sitecore.Configuration.Settings.AliasesActive && global::Sitecore.Context.Database.Aliases.Exists(System.Web.HttpContext.Current.Request.RawUrl))
{
    const string format = "<link rel=\"canonical\" href=\"{0}://{1}{2}\"/>";
    global::Sitecore.Data.Items.Item targetItem = global::Sitecore.Context.Database.GetItem(global::Sitecore.Context.Database.Aliases.GetTargetID(System.Web.HttpContext.Current.Request.RawUrl));

    canonicalLiteral.Text = string.Format(format, "http", System.Web.HttpContext.Current.Request.Url.Host, global::Sitecore.Links.LinkManager.GetItemUrl(targetItem));
}

Note, you may need to adjust that based on the configuration of your LinkManager.

1
  • Great solution!
    – Mark Ursino
    Nov 16, 2011 at 17:37
2

Regardless of whether or not an item has any aliases, you will most likely need the canonical URL of the context item anyway; Therefore, the solution is even simpler:

var options = LinkManager.GetDefaultUrlOptions();
options.AlwaysIncludeServerUrl = true;
canonicalLiteral.Text =
Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Item, options);
1

Use Sean's answer, but I would use LinkManager to build the entire url:

var options = LinkManager.GetDefaultUrlOptions();
options.AlwaysIncludeServerUrl = true;
canonicalLiteral.Text = LinkManager.GetItemUrl(global::Sitecore.Links.LinkManager.GetItemUrl(targetItem));

Your Answer

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