Try our conversational search powered by Generative AI!

HXH
HXH
Apr 30, 2010
  7433
(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 and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog