Try our conversational search powered by Generative AI!

Deane Barker
Jul 1, 2010
  6636
(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
Search & Navigation: Indexing job new features

From Episerver.Find version 16.1.0, we introduced some new features that make the indexing job in CMS more flexible and efficient: Support continuo...

Vinh Cao | May 7, 2024

Exclude content from search engines

Best practices for excluding your Optimizely CMS test content from search engines like Google, along with some tips on what to do—and what not to...

Tomas Hensrud Gulla | May 7, 2024 | Syndicated blog

Run imgproxy container on Azure App Service with Front Door CDN

A simple way to host a imgproxy Docker container and use it to get AVIF and WEBP images.

Johan Kronberg | May 5, 2024 | Syndicated blog

Why C# Developers Should Embrace Node.js

Explore why C# developers should embrace Node.js especially with Optimizely's SaaS CMS on the horizon. Understand the shift towards agile web...

Andy Blyth | May 2, 2024 | Syndicated blog