With the release of Sitecore 7.2 encode name replacements now applies to media URL’s, prior to 7.2 it only applied to content items, and therefore media URL’s with spaces used to be like the following:
But with Sitecore 7.2, they can be as follows:
This change in fact caused all the images on the site I was upgrading to disappear, but first a quick introduction to Encoding name replacement.
It provides the ability to specify text replacements to use when the media manager generates the friendly URL for a media item. The element /sitecore/encodeNameReplacements in the web.config; contains a number of replace elements and each replace element defines what to find and replace.
<encodeNameReplacements> <replace mode="on" find="&" 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>
Strangely enough replacing a space with a dash is not defined by default, but I have added it to almost every solution as %20 in URL’s kills SEO. Below is the element you need to add to achieve that spaces are replaced by dashes.
<replace mode="on" find=" " replaceWith="-" />
Nemesis and side effects
I have reported to Sitecore support a number of times that I thought that encode replacements should apply to media URL’s. So with 7.2 I got my wish and this is where nemesis strikes me as after upgrading to Sitecore 7.2 all the product images disappeared from the site.
The solution has over 50000 images which are synchronized from their PIM (Product Information Management) and name of media item had a space and a dash.
If you want to have spaces replaced with dashes the one restriction is that you can no longer have dashes in the items name. Because when Sitecore tries to resolve the URL it applies the reverse of any encode replacements i.e. sitecore replace all dashes with a space to find the item path.
For example assume that your media item has a space and a dash in its name.
/media library/cars/myCar 26-10-2013
The URL will be
But when Sitecore tries to resolve the URL to find the image it replaces all the dashes with spaces (i.e. the reverse of the encode name replacement) and then it can’t find the item at the following path.
/media library/cars/myCar 26 10 2013
There are a number of solutions
- Write script to replace all dashes with a space in the media library
- Create a custom media provider, which doesn’t execute the encode replacements.
Whist I try to avoid modifying standard sitecore behavior in this case I choose to override the GetMediaUrl of the MediaProvider, as it is not possible to change the image names due to the business requirements of the site.
Anyway I hope this helps anybody if there images disappear after upgrading to 7.5.