How To Define the page URL for a Sitecore page

Having well named Url’s on your website is key to SEO and getting traffic to your site. In today’s post, I’m going to cover the basics of how to set pages Url in Sitecore and what some of the options are to customise them to fit individual needs.

How To Define the URL of a page

In Sitecore Url’s are created by the name a content editor gives to pages in the content tree.

sitecore_page_urls1

The page name is directly translated to a Url, so a page under the ‘Home’ node called, ‘NewPage’ would be translated to www.website.com/NewPage. As the page name is used as the Url,  it means that page name can only contain characters that create a valid URL.

It’s important to understand that if the page name changes, then the Url for the page will as well, as well as all of its children. This is an important fact to remember when editing content, as renaming a page might cause SEO issues and can result in losing page rankings in Google, existing Url’s that point to the page will break, etc..

If you want your page to be called something like ‘Questions & Answers’ you can use the page title (rather than the page name) field defined in the data template to display the text on a page. The Url/Page Name always has to have valid Url characters.

What happens if my page name contains a space in it?

Say you have a page called ‘Contact Us’. If you called the page that, the Url Would look something like this:

www.website.com/contact%20us

In this instance’s you may want to add custom rules about how a page name translates to a Url. If you look in your web.config files, you should see a section called ‘encodeNameReplacements’.

<encodeNameReplacements>
<replace mode="off" find="&amp;" replaceWith=",-a-," />
<replace mode="off" find="?" replaceWith=",-q-," />
<replace mode="off" find="/" replaceWith=",-s-," />
<replace mode="off" find="*" replaceWith=",-w-," />
<replace mode="off" find="." replaceWith=",-d-," />
<replace mode="off" find=":" replaceWith=",-c-," />
<!-- Custom Rules -->
<replace mode="on" find=" " replaceWith="-" />  
<replace mode="on" find="_" replaceWith="-" />  
</encodeNameReplacements>

In here, we can define custom rules to make our Url’s more SEO friendly. In this instance, I’ve created two replacements, one for spaces and one for underscores. If we ran this site the Contact Us Url would now look like this, www.website.com/contact-us.

I Want My Url’s To Be Lowercase

If you want to make your Sitecore URLs’ all lowercase, or, simply do more than just some simple character replacement, you can look at the link manager, again configured in the web.config.

<linkManager defaultProvider="sitecore">
<providers>
<clear />
<add name="sitecore" type="Sitecore.Links.LinkProvider, Sitecore.Kernel"     
addAspxExtension="false"
alwaysIncludeServerUrl="false"
encodeNames="true
languageEmbedding="always"
languageLocation="filePath"
lowercaseUrls="true"
shortenUrls="true"
useDisplayName="false" />
</providers>
</linkManager>

In here you can set Sitecore to display the aspx extension, treat URLs as lowercase and a whole other bunch of options.

I want to customise my Url’s further

So far, we’ve covered a few options that will cover the majority of users. If you still need extra customization you can write your own rules in Code and hook into the Sitecore pipeline. In the code snippet below, I’m creating a simple rule to enforce all Url’s are lower case on the site.

using Sitecore.Configuration;
using Sitecore.Data.Items;
using Sitecore.Links;
public class CustomLinkProvider : LinkProvider
{
public override string GetItemUrl(Item item, UrlOptions urlOptions)
{
urlOptions.SiteResolving = Settings.Rendering.SiteResolving;
return base.GetItemUrl(item, urlOptions).ToLower();
}
}

In the Link Manager config in your website, simply point it to your new class.

<linkManager defaultProvider="sitecore">
<providers>
<clear />
<add name="sitecore" type="namespace.CustomLinkProvider, AssemblyName"     
addAspxExtension="false"
alwaysIncludeServerUrl="false"
encodeNames="true
languageEmbedding="always"
languageLocation="filePath"
lowercaseUrls="true"
shortenUrls="true"
useDisplayName="false" />
</providers>
</linkManager>

Handling Mispelt Urls’

Ok, so we’ve handled and simplified the Url structure within Sitecore, so when Sitecore renders content links they will always render as lowercase. However, just because Sitecore is working with lowercase Url’s it can still accept requests with any combination of upper and lower case characters. If we wanted to try and help our users out, we could take this a step further and hook into the Sitecore pipeline to ensure that all Url’s (internal AND external) that might, accidently, have upper case characters in the Url to ensure they resolve correctly.

The easiest way to do this in IIS. You can use the IISRewrite module to enforce that all Url’s that are passed to Sitecore are all lowercase. The purpose of this snippet is to give you some ideas about what can be done, rather than a best practice technique. Personally, to solve the lowercase issues I would always use IIS.

using System.Web;
using Sitecore.Pipelines.HttpRequest;
public class LowercaseResolver : ItemResolver
{
public override void Process(HttpRequestArgs args)
{
if (Sitecore.Context.Item == null)
return;
if (!IsUpperCase(HttpContext.Current.Request.RawUrl))
return;
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.RawUrl.ToLower());
}
public bool IsUpperCase(string url)
{
for (int i = 0; i < url.Length; i++)
{
if (url[i] > 64 && url[i] < 91)
return true;
}
return false;
}
}

First, we inherit from ItemResolver and override the Process method. In here we check that the Url doesn’t contain any Upper case characters, if it does we convert the Url to lower case.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<httpRequestBegin>
<processor type="NameSpace.LowercaseResolver, AssemblyName" patch:after=
"*[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, 
Sitecore.Kernel']"/>
</httpRequestBegin>
</pipelines>
</sitecore>
</configuration>

In order for the code to be picked up by Sitecore, we need to register the class in the httpRequestBegin pipeline. This is quite a simple example but it shows you the concepts of dealing with external Url’s, if the need arises.

Customising Url’s with Extensions

I have no idea why you would ever need to do this, but, in Sitecore you can prefix your Url with any file extension you want. To do this, look in the preprocessRequest in your web.config

<preprocessRequest help="Processors should derive from Sitecore.Pipelines.PreprocessRequest.PreprocessRequestProcessor">
<processor type="Sitecore.Pipelines.PreprocessRequest.SuppressFormValidation, Sitecore.Kernel" />
<processor type="Sitecore.Pipelines.PreprocessRequest.NormalizeRawUrl, Sitecore.Kernel" />
<processor type="Sitecore.Pipelines.PreprocessRequest.IIS404Handler, Sitecore.Kernel" />
<processor type="Sitecore.Pipelines.PreprocessRequest.FilterUrlExtensions, Sitecore.Kernel">
<param desc="Allowed extensions (comma separated)">aspx, ashx, asmx</param>
<param desc="Blocked extensions (comma separated)">*</param>
<param desc="Blocked extensions that stream files (comma separated)">*</param>
<param desc="Blocked extensions that do not stream files (comma separated)">
</param>
</processor>
<processor type="Sitecore.Pipelines.PreprocessRequest.StripLanguage, Sitecore.Kernel" />
</preprocessRequest>

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *