in this tutorial, you will learn the basics of how to set pages Url in Sitecore and what some of the options are to customise them to fit individual needs. If you are new to Sitecore mastering Urls will be key to ensuring your site has good SEO. If you are struggling with Urls and Sitecore, this is the tutorial for you π₯π₯π₯
How To Define the URL of a page
In Sitecore, a page Url is defined by the name a content editor gives to pages within the Sitecore editor:
The page name property is used to generate the page Url. An object created under the Home
node called NewPage
, would have a Url www.website.com/NewPage
. As the name is used to generate the Url, it means the page name property can only contain characters that are allowed within a valid URL.
It's important to understand that if the objects name changes within the CMs, its Url will change. If that page has any child pages, their URLs will be impacted as well π€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. If any editor has manually linked to that page using the URL, those links will break π
Within Sitecore, you also have access to a related page property called page title
. The page title
property is not used for the Url so it can contain non Url restricted characters. Page Title can be used to populate the pages meta-data title
property and even the pages h1
tag.
What Happens If My Page Name Contains A Space?
Say you have a page called Contact Us
. By default, Sitecore would convert Contact Us
into this Url www.website.com/contact%20us
. All spaces will be swapped to %20
. Having %20
in a URL is not ideal. %20
is a pain to type and makes a Url harder to understand.
If you are unhappy about the way Sitecore translates names to Urls, Sitecore allows you to customise the mapping rules. You can add a custom rule that tells Sitecore exactly how you would like it to translate a name to a Url.
To do this, look within your web.config
file. You should see a section called encodeNameReplacements
, like this:
Here, you can define custom rules to make your Url's look more SEO friendly. In this example, I've created two rules. One for spaces and one for underscores. With these rules added to the config, 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 force all of your Sitecore URLs' to be lowercase, or, you want to do something more powerful to the Url than a simple character replacement, you can look at the link manager. The link manager is also configured within web.config
:
Within here you can configure Sitecore to do some pretty powerful things π
I Want To Customise My Sitecore Url's Even Further
So far, we've covered some configuration options that will cover the majority of use cases you will encounter. If you still need extra customization, you can write your own rules in code and hook them into the Sitecore pipeline so the rules get run whenever a page request is made. Being able to write code to define rules can be very handy! In the code snippet below, I'm creating a simple rule to enforce all Url's on the site to be lowercase:
To make Sitecore run this code, within the LinkManager
config, point it to your new class:
Handling Misspelt Sitecore Urls
In the snippet above, we changed how URLs are generated. This is great. however, what happens if your tweaks now cause Sitecore to 404 when a page request is made. In Sitecore you can also create ItemResolvers
. These resolvers are handy as you can ensure a Url correctly resolves to the Sitecore item that you want to trigger. Instead of changing a URL you can ensure that it resolves correctly instead! The code to do this is shown below:
To create a resolver, create a class and make it inherit from ItemResolver
. Override the Process()
method. In here you check the Url and map it accordingly:
When creating custom resolvers, you will need to register them within httpRequestBegin
. Without this config, your resolver will never be called. If your resolver is not working, check this config out first!
Customising Url's with Extensions
Finally, in Sitecore, you can prefix your Url with any file extension you want. To do this, look in the preprocess requests in your web.config
I've never needed to use this technique, however, this knowledge might help one reader out with some bespoke requirements π
You are now a Sitecore CMS Url Guru π§π§. You know all things Url in Sitecore. Go and create beautiful SEO friendly websites. Happy Coding π€