November Happy Hour will be moved to Thursday December 5th.

HXH
HXH
Apr 30, 2010
  7542
(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();
            }
            else
            {
                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

Comments

valdis
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
Optimizely SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog