Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 4 down vote accepted

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.

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.