Deane Barker
Jul 1, 2010
  6660
(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
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024