Friendly URLs

Friendly URLs

Friendly URLs

  • Do not contain spaces - a space in an item’s name gets encoded as %20
  • Include information in the URL path instead of using a query string parameter
  • Developers can convert unfriendly URLs to user friendly URLs by writing code to replace the spaces with dashes

Optimizing URLs in Sitecore for SEO

  • Organize site so URLs provide topical categorization of pages
  • Replace query string parameters with dynamic URLs
  • Remove file extensions from URLs, such as .aspx and .ashx, except for documents (.pdf)
  • Add a slash character (“/”)  at the end of URLs
  • Avoid multiple URLs for a single content or media item
  • Use Sitecore Online Marketing Suite (OMS) so all URL forms of items get resolved

Getting Friendly URLs

  • Sitecore references items using their unique IDs rather than their paths
  • FieldRenderer or the renderField pipeline is used to transform IDs to friendly URLs
  • Use the GetItemUrl() method to access the friendly URL of a content item
  • The code snippet below accesses the friendly URL of the context item

GetItemUrl Method

  • Method in Sitecore.Links.LinkManager class that gets the friendly URL of an item
  • Does not use the Rendering.SiteResolving setting in web.config to include hostnames in URLs

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Items.Item
  • Sitecore.Data.Managers.LanguageManager
  • Sitecore.Links.LanguageEmbedding
  • Sitecore.Links.LanguageLocation
  • Sitecore.Links.LinkManager
  • Sitecore.Links.LinkUrlOptions

Friendly URLs C# Web Control Code

Sitecore.Data.Items.Item item = Sitecore.Context.Item;
if (item == null) return;

Sitecore.Links.UrlOptions urlOptions = Sitecore.Links.UrlOptions.DefaultOptions.Clone() as Sitecore.Links.UrlOptions;
urlOptions.AddAspxExtension = false;
urlOptions.AlwaysIncludeServerUrl = true;
urlOptions.LanguageLocation = Sitecore.Links.LanguageLocation.QueryString;
urlOptions.LanguageEmbedding = Sitecore.Links.LanguageEmbedding.Always;
urlOptions.ShortenUrls = true;
urlOptions.UseDisplayName = false;

string friendlyUrl1 = Sitecore.Links.LinkManager.GetItemUrl(item);
string friendlyUrl2 = Sitecore.Links.LinkManager.GetItemUrl(item, urlOptions);

urlOptions.LanguageLocation = Sitecore.Links.LanguageLocation.FilePath;
string friendlyUrl3 = Sitecore.Links.LinkManager.GetItemUrl(item, urlOptions);

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

output.Write("{0}&#58;&#160;{1}", "<p>Friendly Url Without Setting URLOptions", "<br/><b>" + friendlyUrl1 + "</b></p>");
output.Write("{0}&#58;&#160;{1}", "<p>Friendly Url With Language Embedding Using Query String", "<br/><b>" + friendlyUrl2 + "</b></p>");
output.Write("{0}&#58;&#160;{1}", "<p>Vanity Url With Language Embedding Using File Path", "<br/><b>" + friendlyUrl3 + "</b></p>");
output.RenderEndTag(); //Div

Sources