Try our conversational search powered by Generative AI!

HXH
HXH
Apr 30, 2010
  7438
(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
Azure AI Language – Extractive Summarisation in Optimizely CMS

In this article, I demonstrate how extractive summarisation, provided by the Azure AI Language platform, can be leveraged to produce a set of summa...

Anil Patel | Apr 26, 2024 | Syndicated blog

Optimizely Unit Testing Using CmsContentScaffolding Package

Introduction Unit tests shouldn't be created just for business logic, but also for the content and rules defined for content creation (available...

MilosR | Apr 26, 2024

Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024