Improved support for canonical URLs

February 12, 2015

Search engines often highlights the importance of using canonical URLs to signify the preferred URL that should be used when the same content can be reached using multiple URLs.

And while EPiServer has always supported the use of canonical URLs it is now easier than ever to setup multiple domains and ensure that there is only one canonical content URL from a search engine perspective. This includes adding canonical link elements to the HTML head of your pages and redirecting requests to your primary domain.

Site definition changes

The first part of this feature is the support on the SiteDefinition structure to define which host names that should be considered primary. You can set multiple host names as primary, but there can only be one primary host per culture. Primary host names can also be configured in the CMS administration interface.

There is also an option to set a host name to be redirected to the primary domain. Redirects can be set to permanent or temporary, but in most cases it is recommended that you use permanent redirects. Note that redirects only affects content requests.

If you are setting up a redirected host, you must always have at least one non-redirected host configured for the same culture. It is recommended that you identify the primary host to where any redirected request should be sent, but in case this has not been done one of the other (non-redirected) hosts will be used instead.

The host type can also be left undefined in which case users won’t be redirected and remain on the requested site. If you have upgraded a site from a previous version, this will be how the host names are treated.

Generating URLs

As always when generating a URL to a page you want to make sure that you are using the UrlResolver.GetUrl() method either directly or indirectly. By passing in a VirtualPathArguments instance with ForceCanonical set to true as an argument you can make sure that you get the canonical URL even if you currently are visiting the site using an alternative host name.

var url = UrlResolver.Current.GetUrl(ContentReference.StartPage, “en”, new VirtualPathArguments { ForceCanonical = true });

Regardless of arguments, this method will always return the absolute canonical URL if it’s called outside of a web request, such as from a scheduled job.

Adding a canonical link element

When developing a site you always want to make sure to add a canonical link element to the <head> section of all your pages such as:

<html>
  <head>
    <title>…</title>
    <link rel="canonical" href="http://example.com/my-page" />
  </head>
  <body>…</body>
</html>

This will ensure that search engines knows which URL should be used to reach a certain page. This is not only important if your site is using multiple host names, but also if you have a single domain site as not only differences in host names are taken into consideration but also differences in casing or trailing slashes when ensuring that the same content won’t show multiple times. For more information, see Google Webmaster Tools Help.

Even though the UrlResolver make it easy to generate a canonical link, there is also a HTML helper for MVC and a web control for WebForms to make things even easier.

MVC Razor

@Html.CanonicalLink()

WebForms

<EPiServer:CanonicalLink runat=”server” />

How about performance?

This feature was first and foremost developed with the idea of making canonical URLs easier to manage from within EPiServer. Even though there should be no concerns over using this feature in most scenarios, if performance is critical on your site and you are expecting a vast number of request to hosts that should be redirected, it’s recommend that you setup HTTP redirects directly in IIS or in your load balancer as this means that no part of ASP.NET or EPiServer has to be engaged for these requests.

Advertisements
%d bloggers like this: