1

I am new to Apache mod rewrite. What I would like to achieve is this.

If an image (gif, jpg, png) is requested from an url and is not found there, it should automatically try to deliver this image from another directory.

user9517
  • 115,471
  • 20
  • 215
  • 297
clamp
  • 175
  • 1
  • 7

1 Answers1

5

Try this:

RewriteEngine on
# if there's no file at the requested path..
RewriteCond %{REQUEST_FILENAME} !-f
# then, if it's an image file, try the other path:
RewriteRule /([^/]*\.(gif|png|jpg))$ /images/path/$1

This will act on every image file request that these rules are applied to; to restrict to just a specific "original" directory, then adjust as needed:

RewriteRule ^/old/images/path/([^/]*\.(gif|png|jpg))$ /images/path/$1

And if you're putting this in .htaccess or changing the RewriteBase, change accordingly.

Shane Madden
  • 114,520
  • 13
  • 181
  • 251
  • thanks, yes i am putting this in a .htaccess file which is in the root of the website's directory on the server. now should the rewritebase be / or /pathto/websitebase/ – clamp Sep 16 '11 at 12:12
  • @clamp That `.htaccess` will give you a `RewriteBase` of `/`, which will work just fine. You'll just want to change the rules that you insert to work correctly in that context; so, for instance with the second rule example I gave, you'd change it to just strip the leading slashes: `RewriteRule ^old/images/path/([^/]*\.(gif|png|jpg))$ images/path/$1` – Shane Madden Sep 16 '11 at 14:23