No images after upgrading to Sitecore 7.5

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:

/-media/some%20item%20with%20space/test-xx-yy.jpg

But with Sitecore 7.2, they can be as follows:

/-media/some-item-with-space/test-xx-yy.jpg

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="&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>

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

/~media/cars/myCar-26-10-2013.jpg

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.

3 thoughts on “No images after upgrading to Sitecore 7.5

  1. Pingback: SEO Friendly URLs in Sitecore – Prevention is Better than Cure | jammykam

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