Try our conversational search powered by Generative AI!

Deane Barker
Jul 1, 2010
  6639
(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
From Procrastination to Proficiency: Navigating Your Journey to Web Experimentation Certification

Hey there, Optimizely enthusiasts!   Join me in celebrating a milestone – I'm officially a certified web experimentation expert! It's an exhilarati...

Silvio Pacitto | May 17, 2024

GPT-4o Now Available for Optimizely via the AI-Assistant plugin!

I am excited to announce that GPT-4o is now available for Optimizely users through the Epicweb AI-Assistant integration. This means you can leverag...

Luc Gosso (MVP) | May 17, 2024 | Syndicated blog

The downside of being too fast

Today when I was tracking down some changes, I came across this commit comment Who wrote this? Me, almost 5 years ago. I did have a chuckle in my...

Quan Mai | May 17, 2024 | Syndicated blog

Optimizely Forms: Safeguarding Your Data

With the rise of cyber threats and privacy concerns, safeguarding sensitive information has become a top priority for businesses across all...

K Khan | May 16, 2024