Sitecore media url - remove ashx extension

Have you ever marked all your media items are rendered in our page output as .ashx extension? By default, Sitecore renders all media files or images (.jpg, .gif, .pdf, .xls, etc.) as .ashx extension.

The same thing you will find while getting mediaurl for your media items.

You can render media with its own extension using Media.RequestExtension setting in web.config.
It defines the extension to use in media request URLs. It's default its value is ashx, so all media url are .ashx.
So, media's URL will be: http://mydomain.com/~/media/Images/Banner/image.ashx
<setting name="Media.RequestExtension" value="" />

Setting its value to blank will render media url with relevant extension. Now, all your media items will be rendered in page output with its valid extension like, http://mydomain.com/~/media/Images/Banner/image.jpg
Still, your existing URL http://mydomain.com/~/media/Images/Banner/image.ashx will also work.

Image url can have different querystring parameters. Read more for Image Control and QueryString parameters.

4 comments:

  1. Thanks for your blog post. I have an issue, however, and I was wondering if you would know what could be causing it. When I set this value to empty string, it does render as a png extension and everything works locally fine, but when I publish this code to my dev server, all the media items give a 404 because it's looking for the png file on the physical hard drive and doesn't trigger sitecore's pipelines. What settings would I need to change on my dev server for this to work?

    ReplyDelete
    Replies
    1. Hi Chad,
      I don't think it's because of the extension on your dev env.
      It might be because, your media handler is not configured properly. You can try debuggin CustomHandler () and see your ~/media/ handler.
      I just checked your comment and wish you have solved this issue. If it's still not solved, you can drop an email to me with your web.config file, will surely look for that.

      Delete
  2. Any hints at what the advantages might be to specifying the real extension instead of ashx? Does this presumably help search engines/crawlers to determine what is actually an image (or other file type) on your site, or does the ashx provide the appropriate mime type once processed and thus it doesn't really matter? Just wondering what exactly this provides in value over leaving it as an ashx?

    ReplyDelete
    Replies
    1. There is no difference in both approach. It does not make any difference for search engines or browsers as they identify media by mime types from response headers, and both approaches serves same content and response headers.

      The only advantage using appropriate extension gives is easiness in writing caching rules on CDN/reverse proxy. Just to note, it's also possible to write caching rules using mime types when ashx extension used.

      Delete