Adding Canonical Links for Language-Specific URLs
We’re doing an multi-lingual site, and one of the requirements is that we put the language identifier in the URL. So URLs will look like:
And, of course, this URL is also valid:
This always makes me nervous because I start worrying about Google and other search crawlers. I don’t want content indexed under more than one URL.
Google has a solution for this -- their “canonical” link tag, as described here:
If your site has identical or vastly similar content that's accessible through multiple URLs, this format provides you with more control over the URL returned in search results. It also helps to make sure that properties such as link popularity are consolidated to your preferred version.
So, I wrote up a little page plugin to automate this. This code executes during PreRender for any EPiServer content. It gets the list of enabled languages, and if the RawUrl begins with any of the enabled languages (“/en/”, “/fr/”, etc.), it inserts a LINK tag in the header with the non-language specific URL.
So a request for this --
-- would result in this being added to the header:
<link href=”/some-page” rel=”canonical”/>
A small thing, but important for all the reasons Google mentions in the article linked above.
Here’s the code. It’s a single class file. Compile it into your project. (And, all the standard warnings apply – hasn’t been production tested, use at your own risk, yada, yada…)