Skip to main content

Transform links in HTML to friendly URLs

Abstract

How to transform links in HTML to friendly URLs using the FieldRenderer web control.

You can use the FieldRenderer Web control, the renderField pipeline, or the Sitecore.Links.LinkManager.ExpandDynamicLinks() method to transform dynamic links into friendly URLs.

You can use the Sitecore.Links.LinkManager.ExpandDynamicLinks() method to transform dynamic links in Rich Text Editor (RTE) fields, including both content and media links, to friendly URLs. For example, to transform dynamic links in the Text field in the /Sitecore/Content/Home item in the Master database:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");

string containsDynamicLinks = home.Fields["text"].Value;

string containsFriendlyLinks = Sitecore.Links.LinkManager.ExpandDynamicLinks(

containsDynamicLinks, Sitecore.Configuration.Settings.Rendering.SiteResolving);

string finalMarkup = System.Text.RegularExpressions.Regex.Replace(containsFriendlyLinks,

"([^/])~/media", "$1/~/media");