Try our conversational search powered by Generative AI!

Deane Barker
Jul 1, 2010
  6632
(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 Forms - How to add extra data automatically into submission

Some words about Optimizely Forms Optimizely Forms is a built-in add-on by Optimizely development team that enables to create forms dynamically via...

Binh Nguyen Thi | Apr 29, 2024

Azure AI Language – Extractive Summarisation in Optimizely CMS

In this article, I demonstrate how extractive summarisation, provided by the Azure AI Language platform, can be leveraged to produce a set of summa...

Anil Patel | Apr 26, 2024 | Syndicated blog

Optimizely Unit Testing Using CmsContentScaffolding Package

Introduction Unit tests shouldn't be created just for business logic, but also for the content and rules defined for content creation (available...

MilosR | Apr 26, 2024

Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

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