Try our conversational search powered by Generative AI!

Deane Barker
Jul 1, 2010
  6629
(3 votes)

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:

/en/some-page
/fr/some-page

And, of course, this URL is also valid:

/some-page

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 --

/en/some-page

-- 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…)

Jul 01, 2010

Comments

Ted
Ted Sep 21, 2010 10:33 AM

Great!

Canonical tags are often overlooked, and it's especially problematic when fallback languages are used.

We used a similar approach in the MasterPageBase class in EPiServer Template Foundation. However, we also check to see if the page uses "Fetch data from", in which case the canonical URL is set to the source page.

Sep 21, 2010 10:33 AM

This could be very useful. :)
/ Cool

Sep 21, 2010 10:33 AM

Very nice!

Sep 21, 2010 10:33 AM

Its worth thinking about the fact that translated content may actually be different (and should certainly be in a different language). In canonical links I've implemented I check if the page is falling back to the master language. If it is falling back then add a canonical link, if not then do not add a link as we want Google to index the translated content.

Sep 21, 2010 10:33 AM

Hi,

Nice article.
As part of our project implementation i have used your given code.
Our url pages are in "en" and "en-gb" versions. The code is helpful in adding the Link tag but it is not modifing the Url. Can you please tell me do i need to add any code extra to append/modify the Url?
/ Sharath

Nov 3, 2010 03:06 PM

Thanks for sharing this.

Please login to comment.
Latest blogs
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog