While Sitecore’s configuration settings can be tweaked to produce lowercase, dash-separated URLs via LinkManager, savvy Marketers are going to want visual confirmation of a given Item’s URL. They’ll also want you to make sure no one screws up when creating Item URLs on the fly.
Constellation.Feature.UrlFriendlyPageNames can help
What It Is
A very small library that installs a new Rule Action into Sitecore that allows you to “tweak” Item names to make them look like URL “slugs” within Sitecore.
The Rule Action has the following options that will be used to transform an Item’s name on save:
- Replace Diacritics
- Clear Display Name
- Remove (specific) Illegal Characters
- Remove Spaces
- Force Lowercase
In Visual Studio fire up Package Manager and install the NuGet package to your .NET Web Application project:
PM> Install-Package Constellation.Feature.UrlFriendlyPageNames
The package contains a DLL for the custom Item renaming action as well as a Sitecore Package (*.zip file) that defines the Action in the Sitecore System folder. The Sitecore Package will self-install when you deploy your solution.
Identifying an Item as a “Page”
While you could apply this rule globally, in reality only Items that can appear in a browser’s address bar require URL tweaking. We typically limit this rule to “Pages”; Items that have Presentation and are therefore “surfable.” To limit the scope of this rule, you’ll need to be able to identify these Items within your Sitecore installation. This is typically done by using a “flag” Data Template, either a base template, or some other contract template that all Pages inherit from for the purposes of identifying them as Pages to the Rules engine.
Deciding on your URL structure
Most SEO gurus will agree that your URLs should be composed of only lowercase letters, particularly on Windows, which is not case sensitive. The Rule Action in this component can be configured to lowercase automatically or to retain the original mixed-case name.
Most SEO gurus will agree that a page name where the spaces are replaced by dashes is the most human-legible standard. The Rule Action in this component can be configured to replace spaces with dashes automatically, or to remove spaces altogether.
While not common in American English, diacritics refers to the accent characters that may appear above or below certain letters in many languages. While they may be allowed in URLs, they may make the URLs harder to type, and they may get stripped out in certain circumstances. The Rule Action in this component can be configured to remove diacritic marks, reducing the character to its minimal ASCII a-zA-Z style.
Remove Illegal Characters?
Although Sitecore already does a pretty good job of removing non-word characters from Item Names, those rules can be relaxed in a particular instance, or they may be inadequate. (For example “_” is perfectly legal in a Sitecore Item name, but is bad practice in URLs.) You can use a RegEx expression in the Rule Action to remove characters you consider “illegal” from the Item’s Name. We suggest the safe default of “\W” which removes all “non-word” characters from your Item Name.
Clear Display Name?
Sitecore’s Display Name feature allows for the Content Tree to list regionalized (translated) Item names in the tree without affecting the Items’ URLs. Unfortunately, the Display Name property of an Item completely overrides the Item Name in all aspects of the Sitecore Content Editor, making the actual Item Name (and therefore URL) all but impossible to spot for non-Admin users. The Rule Action in this component can be set to clear the Display Name of the Item it is grooming, forcing the Content Tree to display the URL slug, which is in our humble opinion, a more useful thing to Sitecore Marketers.
Configuring the Rule
- The rule needs to be created in the Item Saved folder of the System/Settings/Rules area of Sitecore.
- You may need to create a custom Rule Condition to identify your Page Items and limit the scope of this rule.
- Add the Set URL-Friendly Item Name action to your rule.
- Configure the options on the Action to match your URL policy.
Here’s an example of a Rule where the Condition limits the rule to Items that are “Context Datasource” types (they inherit from a Template called Context Datasource):
- Decide how you will classify Items as Pages that need URL grooming.
- Decide on your URL strategy.
- Create a Rule to enforce it.