advanced web statistics

Dynamically Insert Language ISO Code in Sitecore Url

4/29/2010 11:55:39 AM

In working with localizing a Sitecore CMS solution recently I came across an interesting requirement: analytics (SEO optimization by region).  I was handling Sitecore localization by adding supported languages and creating localized versions of each content item.  The wrinkle was that I needed to now add a language prefix to all the urls on the site so they could get indexed appropriately for each region.  To make it even more complicated, I had to deal with hard-coded urls that weren't driven by the CMS.  I thought I was going to have to create multiple sites within Sitecore for each language, duplicate content, etc...  It turns out that it was much easier than this.

In a Sitecore web.config you'll find a linkManager section. This will handle much of the heavy lifting for you. There is one setting that you need to pay attention to (2 if you aren't using IIS 7).

The first (and most important) is the languageEmbedding setting. In a site that is in one language you should set to "never". A site that may have one or two localized pages here and there set to "asNeeded". In my case, a full-blown localization effort, this should be set to "always".

If you aren't using IIS 7 you will need to set the setting for addAspxExtension to "true".

    <linkManager defaultProvider="sitecore">
      <providers>
        <clear />     
          <add name="sitecore" type="Sitecore.Links.LinkProvider, Sitecore.Kernel"
          addAspxExtension="true"
          alwaysIncludeServerUrl="false"
          encodeNames="true"
          languageEmbedding="always"
          languageLocation="filePath"
          shortenUrls="true"
          useDisplayName="false" />
      </providers>
    </linkManager>

Sitecore CMS will now take care of the url rewriting!  Easy.

For those you like me that have pre-defined navigation requirements (i.e. not driven 100% by the CMS) there is one more step to take to accomplish url prefixing.  Take a link such as:

<a href="/Foo.aspx">

To get a url that looks like http://www.foo.com/de-DE/foo.aspx you simply need to write a little bit of "yellow code".

<a href="/<%= Sitecore.Context.Language %>/foo.aspx">

Easy.

Sitecore

kick it on DotNetKicks.com

Leave a Comment

   

  Enter the text to proceed!