Apr 30, 2010
(0 votes)

Getting Friendly, International URLs

It’s generally best practice to display friendly URL’s to web visitors instead of the standard PageLink attribute that the system uses (A URL like http://www.somesite.com/en-us/MyProductCategory/MyProduct/ looks a lot nicer than http://www.somesite.com/PageTemplates/HomePageTemplate.aspx?id=643&epslanguage=en-US)

To that end, I often find myself using a procedure called GetFriendlyURL, which takes a PageData object and a Boolean value, and returns a friendly URL based on the UrlRewriteProvider.ConvertToExternal function. It goes something like this:

        public static string GetFriendlyUrl(PageData pd, bool Absolute)
            UrlBuilder url = new UrlBuilder(pd.LinkURL);
            EPiServer.Global.UrlRewriteProvider.ConvertToExternal(url, pd.PageLink, UTF8Encoding.UTF8);
            if (!Absolute)
                return url.ToString();
                return GetBaseUrl() + url.ToString();

Unfortunately, I’ve noticed that this procedure tends to always return the link in the form that the PageData object was originally created in… so if I’m on a different locale, I’ll often get links returned that include the /en-us/ (our base language) even though they should technically be returning links in the locale the user is currently in.

I address this issue with a simple string replacement procedure, as seen below:

        public static string FetchInternationalLink(String InternationalLink, String LanguageVersion)
            return InternationalLink.Replace("en-us", LanguageVersion.ToLower());

Voila – clean, friendly, internationalized URL’s in just a few lines of code.

Apr 30, 2010


valdis Oct 24, 2011 05:54 PM

This post seems to be a bit outdated, but anyway. I ran into similar issue and found that better approach is to use:

new UrlBuilder(UriSupport.AddLanguageSelection(pd.LinkURL, pd.LanguageBranch));

That will add/set correct language query parameter to the link and will workout fine during link transformation to friendly URL.

Please login to comment.
Latest blogs
Customizing Property Lists in Optimizely CMS

Generic property lists is a cool editorial feature that has gained a lot of popularity - in spite of still being unsupported (officially). But if y...

Allan Thraen | Oct 2, 2022 | Syndicated blog

Content Delivery API – The Case of the Duplicate API Refresh Token

Creating a custom refresh provider to resolve the issues with duplicate tokens in the DXC The post Content Delivery API – The Case of the Duplicate...

David Lewis | Sep 29, 2022 | Syndicated blog

New Optimizely certifications - register for beta testing before November 1st

In January 2023, Optimizely is making updates to the current versions of our certification exams to make sure that each exam covers the necessary...

Jamilia Buzurukova | Sep 28, 2022

Optimizely community meetup - Sept 29 (virtual + Melbourne)

Super excited to be presenting this Thursday the 29th of September at the Optimizely community meetup. For the full details and RSVP's see the...

Ynze | Sep 27, 2022 | Syndicated blog

Preview multiple Visitor Groups directly while browsing your Optimizely site

Visitor groups are great - it's an easy way to add personalization towards market segments to your site. But it does come with it's own set of...

Allan Thraen | Sep 26, 2022 | Syndicated blog

The Report Center is finally back in Optimizely CMS 12

With Episerver.CMS.UI 12.12.0 the Report Center is finally re-introduced in the core product.

Tomas Hensrud Gulla | Sep 26, 2022 | Syndicated blog