Sitecore Tip #6: Add SEO Value by Replacing Spaces with Dashes in Sitecore
Sitecore page URL is composed from the hierarchical tree structure of its ancestor items. In other words, the relative URL (URL without the domain) would look similar to the item’s path, minus /sitecore/content/home/ or whatever path was chosen for the start item.
Content Editors prefer giving items multi-word names, which makes sense; however, the problem surfaces once you navigate to the item. Once a browser gets a hold of any URL it encodes it, replacing spaces with %20 sequence. Although this does not affect the website performance or functionality, it is very far from being user friendly, which translates into users not being able to –
- tell their location in the website by reading the URL
- easily memorize the URL
In addition, search engines seem to retain the %20 in search result URLs, which simply adds fuel to the fire.
How does Sitecore help resolve URL Space Encoding?
One of the great features of Sitecore is its extensive configurability. The CMS exposes a whole battery of configuration settings via its programmable APIs and configuration files (Sitecore’s web.config is probably the largest one I have personally seen by far).
Sitecore contains an <encodeNameReplacements /> configuration element that hosts multiple self-closing <replace mode=”on” find=”x” replaceWith=”y” />, where x and y are placeholders to be replaced with a string to be found and replaced within paths and URLs. Knowing this we can easily create a patch configuration file that would replace spaces with a dash as follows:
<?xml version="1.0" encoding="utf-8"?> <configuration xmlns:x="http://www.sitecore.net/xmlconfig/"> <sitecore> <encodeNameReplacements> <replace mode="on" find=" " replaceWith="-"/> </encodeNameReplacements> </sitecore> </configuration>
Now not only Sitecore would be able to resolve it to the proper content item, the LinkManager would also respect this setting and modify all your dynamically generated links to include dashes as well. What a great feature!
Sitecore URL Encoding Gotchas
Item names cannot contain dashes
The only caveat with this approach is that item names can no longer have dashes. However, since this is Sitecore we are dealing with, it was kind enough to provide us with another item property that we can use to change the way items names appear in Content Editor – Display Name. In other words, if we wanted to still have a dash in the item name, for instance, “URL-encoded item name”, we would create an item with the name of “URL encoded item name” and later modify its Display Name to include the dash.
Sitecore URL replacement rules are not being applied
This issue often occurs in inherited Sitecore solutions. Many developers tend to override the default LinkManager customizing it to their preference (for instance, I do that to add a trailing slash to the URL without an extension), and for one reason or another set the encodeNames property of the manager to false programmatically, which forces Sitecore to ignore the replacement rules.
You must log in to post a comment.