Canonical URLs help you optimize the ranking of your pages in Internet search engines. Here is how to render canonical URLs in Sitecore 8 XP.

One page, one URL?

When building websites each page has its own URL. That page can be then accessed through this URL. Ideally only one URL is associated with every page and when Internet search engines crawl the contents of your website, they know which URL to return for that page in the search results. In a simple website this might be the case but when building more dynamic websites things look slightly different.

In dynamic websites different URLs might return the same or nearly the same content. Either if you are reusing existing content across your website or returning dynamic content based on some parameters you end up with duplicated content available via different URLs. Although it's not an issue for your visitors, it confuses Internet search engines. As a result you lose control about how Internet search engines rank your content and let them decide how to divide the page ranking.

The good news is, that there is a way to get control over your content back and to instruct Internet search engines how they should index your pages.

Canonical URLs

Canonical URL is the URL that Internet search engines should use to index your page at. Even if the same page is published via many URLs on your website, if all these pages have the same canonical URL, that page will be indexed only once using the URL you specified.

Canonical URLs are rendered in the head section of your webpage:

<link rel="canonical" href="http://www.mavention.com/">

So how would you get the canonical URL of your page in Sitecore?

Rendering canonical URLs in Sitecore 8 XP

As I mentioned before, if you don't reuse content across your website or serve content dynamically, you might not need canonical URL information embedded in your pages. If you however do use the more advanced web content management capabilities of the Sitecore platform, including canonical URLs in your pages' HTML might help you optimize the ranking of your pages in Internet search engines.

Getting item's friendly URL

You can get the URL of your page in Sitecore by using the LinkManager class from the Sitecore API:

Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Item);

Getting item's friendly absolute URL

Calling the GetItemUrl method will return a server-relative URL of your page.

Then in your layout you would call the CanonicalUrl helper to render the canonical URL in your page:

<link rel="canonical" href="@Html.Sitecore().CanonicalUrl()">

This will render the canonical URL in your page's HTML. Unfortunately there are a few more things that you need to take into account if you want to truly leverage Sitecore's capabilities.

Multilingual and canonical URLs

Sitecore supports a number of different ways for building multilingual websites. Depending on your requirements you might for example choose to associate different top-level domains with each language or have them published as a path (ie. /en, /nl).

Sitecore takes your configuration into account and automatically includes it in the URL. If you want to control how your canonical URLs are built yourself, you might want to specify it explicitly in the URL options passed to the GetItemUrl method.

For example, by setting the UrlOptions.LanguageEmbedding property to Never you would instruct Sitecore never to include the language as a part of the URL:

URL aliases and canonical URLs

One of the web content management capabilities provided with Sitecore 8 XP is the ability to specify URL aliases for items. Using aliases you can have multiple URLs associated with the same item. The important part here is that aliases are not redirects: the whole page is accessible through the alias and the alias remains visible as the URL of the page.

Although it might be desirable to use this capability for some scenarios, it results in making the same content available via multiple URLs and fragmenting your page's ranking in Internet search results.

The good news is that if you are rendering canonical URL using the code snippet above, Sitecore will automatically revert to the original URL of the item rather than the alias used to view it. With that there is no additional effort to properly deal with aliases from the SEO point of view.

Inconvenient item clones and canonical URLs

Another web content management capability available with Sitecore 8 XP is the ability to clone items. Cloning items is similar to duplicating with the slight difference that there is a one-way relationship between the source and its clones. Changing the content of the source item can easily be pushed to its clones making it very easy for you to reuse content in your website.

Comparing to aliases clones are different as cloning creates a new item in the content hierarchy. Ideally, assuming that the majority of the content of the cloned item is the same as its source's, you would want to render the canonical URL pointing to the URL of the source item when viewing cloned pages.

Before publishing an item and its clones, it's easy to determine whether an item is a clone or not. If an item is a clone, the item from which it has been cloned can be retrieved through the Sitecore.Context.Item.Source property. Unfortunately, when retrieving this property for the published item, its value is empty.

One workaround around this limitation would be to create a new field in your Data template and in the template's Standard values have the value of that field set to the $id token. When creating a new item that field would be set to the ID of the newly created item. Creating a clone off that item would however leave that value unaltered allowing you to check if the item is a clone or not. Considered this approach you could create the following function to get the source Item:

Canonical query string parameters

One of the reasons for which you might want to use canonical URLs in your website might be that you are loading dynamic content based on some query string parameters. Depending on the parameter the rendered content might differ less or more. To control how your pages are indexed and ranked in Internet search results you would want to tell search engines which query string parameters they should treat as if they returned different pages.

To include canonical query string parameters in the canonical URL we would first need to tell the HTML helper which query string parameters should be included in the canonical URL. You could have those stored along with your website's configuration but you could as well pass them directly into the HTML helper:

Although you might need to adapt this code to your specific requirements, it should be sufficient for the most common scenarios.

Summary

Canonical URLs help you optimize the ranking of your pages in Internet search engines. Sitecore offers rich web content management capabilities that offer you great flexibility with regards to publishing content on your website but which also require you to instruct Internet search engines how to index your web pages. Using canonical URLs you can prevent the same pages on your website being indexed multiple times and fragmenting their page ranking.