0

How can I specify a URL rewrite in my web.config whereby any request for an image is served from a different directory on disk?

The images are located in directory site-root\foo\bar\images. Normally they're accessible by http://site-root/foo/bar/images/

I'd like the images to be accessible via a different URL like http://site-root/img/

For example:

site-root\foo\bar\images\test.png

to be accessed at

http://site-root/img/test.png

I won't really need a filter for file extensions (png, gif, jpg, etc), but just rather any file/resource being requested from img to be read from the real directory.

How can I use ASP.NET web.config URL rewrites, or any other method, to achieve this?

I'm going for a web.config solution, as this will be deployed to Windows Azure Websites.

dav1dsm1th
  • 1,687
  • 2
  • 20
  • 24
p.campbell
  • 98,673
  • 67
  • 256
  • 322

2 Answers2

0

I've achieved this in the past by setting up a virtual directory in IIS to map the URL to a specific folder.

dav1dsm1th
  • 1,687
  • 2
  • 20
  • 24
0

Use this web.config rewrite rule.

<system.webServer>
<rewrite>
  <rules>
    <rule name="Blog Images to Thier Real Location" stopProcessing="true">
      <match url="^img/(.*)" />
      <action type="Redirect" url="foo/bar/images/{R:1}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>
</system.webServer>

From Carlos AG's blog post IIS 7.0 and URL Rewrite

p.campbell
  • 98,673
  • 67
  • 256
  • 322