Sitecore replace spaces with dashes in URL’s

In Sitecore, there is an easy of avoiding the %20 signs in your URL’s. To clarify: If you have an item with a space in the name, the space will appear in the URL as a %20 sign. For example, the item with name “read this” will appear as:

https://yourwebsite.com/read%20this

To avoid this, Sitecore have implemented a simple encodeNameReplacements function. In the sitecore.config, Sitecore have defined a list of character replacements:

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

To make a new replacement, simply add it to the list. In my example, I would like to replace space (” “) with a dash (“-“):

<replace mode="on" find=" " replaceWith="-" />

From now on, all my spaces will be replaced with a dash, and my “read this” page will now have this URL:

https://yourwebsite.com/read-this

BEWARE OF THE INFAMOUS MEDIA LIBRARY REPLACEMENT BUG

In certain versions of Sitecore 7.x and Sitecore 8.1, Sitecore have made a mistake where any item in the media library with a – in the item name will be reversely replaced internally to a space, thus making the URL invalid.

In other words: If your space/dash replacement suddenly breaks media library URL’s, try removing any dashes from the media library item name and see if the problem goes away.

Sitecore support have several bug fixes for this, depending of the Sitecore version.

MORE TO READ:

 

Advertisements

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in Sitecore 7, Sitecore 8, Sitecore 9 and tagged , . Bookmark the permalink.

One Response to Sitecore replace spaces with dashes in URL’s

  1. Casey Royals says:

    ” … Sitecore have made a mistake where any item in the media library with a – in the item name will be reversely replaced internally to a space, thus making the URL invalid” – this has been around so long perhaps it is a feature?? :-(

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s