3

I am just trying to verify the best way to add a self-referening canonical tag to a multilingual Sitecore site.

If I use something like:
<link rel="canonical" href="@Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Item)" />

Do I need to put in any checks for language, aliases, or localization?

For examples if I had a page: www.mywebsite.com/en/organizations

I would want the tag to be:
<link rel="canonical" href="http://www.mywebsite/en/organizations" />

Even if that page had an alias -> so if www.mywebsite.com/orgs was an alias for http://www.mywebsite/en/organizations, the same canonical tag:
<link rel="canonical" href="http://www.mywebsite/en/organizations" /> would be rendered to the page.

Or if I turned this into an AMP page, it would still point back to the original (canonical) URL.

Also, I wonder about the regional sites since I could have this page on a UK site but localized: www.mywebsite.com/en-GB/organisations <- note the 's'

So the correct canonical tag would be:
<link rel="canonical" href="http://www.mywebsite/en-GB/organi**s**ations" />

However, I know that Sitecore will still show a page if there is a US English version and the US non-localized URL is requested. For example, if I accessed this URL:
www.mywebsite.com/en-GB/organizations <- note the 'z'

It won't 404 which is perfectly fine, but I would still want the canonical tag to be the correct localized URL:
<link rel="canonical" href="http://www.mywebsite/en-GB/organisations" /> <- Again note the 's' which is the correct canonical version of this page.

Basically I just want to always return the correct, language/region-aware URL despite any edge cases.

Any insight is much appreciated!

1
  • You can have a custom linkmanger and handle all these scenarios there
    – prabhat
    Sep 20, 2017 at 5:10

1 Answer 1

4

You're taking the correct approach by using LinkManager to generate the URL for the canonical tag. LinkManager should always generate the canonical everywhere a content item is referenced. It's critical that whatever LinkProvider you're using is configured properly:

    <add name="custom" 
     type="Custom.LinkProvider, Custom.Linking" 
     addAspxExtension="false" 
     alwaysIncludeServerUrl="false" 
     encodeNames="true" 
     languageEmbedding="always" 
     languageLocation="filePath" 
     lowercaseUrls="true" 
     shortenUrls="true" 
     useDisplayName="false" 
     siteResolving="true" />
  • If you have more than one site, you absolutely must have siteResolving="true"
  • If your site has more than one language, you must force Sitecore's LinkManager to have the language in the URL, languageEmbedding="always" otherwise Sitecore may generate non-canonical URLs depending on the context of a given Rendering.
  • Any SEO person will also tell you to force lowercase on your URLs. lowercaseUrls="true"
  • Remember that it is possible to have any number of LinkProviders to handle a variety of requirements.

w/regards to URLs that "work" but are not canon, the best way to handle that is with a 301 redirect, either in IIS, before it hits Sitecore, or in Sitecore's HttpRequest Pipeline, just prior to the ItemResolver pipeline handler.

For example, to handle Sitecore's "default English" you could check the URI Path for "/en/", and if it's not present, do a Response.RedirectPermanent() to the URL with the language embedded as it should be.

There are a few other considerations regarding language resolution that are worth noting, because they affect URL generation:

  • Are you going to use "generic" Languages, or "regional dialects"? The difference between en and en-UK will be lost to your content authors. I've generally found that if there is regional content that varies by dialect, it's smarter to insist on the 4-digit ISO code rather than the two digit default. this also makes it easier to align with the visitor's user-agent language where necessary.
  • Are there regional top level domains? This is related to below, as a regional site with only one language will probably resist your requirement to put the language in the URL. You'll have to set the default language for the site, and to suppress the language from being written to the URL via LinkManager you're going to have to ensure that URL writing is always done with the correctly configured LinkProvider for that site.
  • Is there a default language for the site? If so, you will have to modify the language string verifier above to check against the language that Sitecore has already identified in the LanguageResolver HttpPipeline Processor. Again, a 301 should be called if the expected language is not in the string. As a rule, language in the URL overrides any defaults if you're using Sitecore's stock LanguageResolver, therefore the URL and the Context.Language should agree after LanguageResolver is called.

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.

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