Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

1 Answer 1

up vote 3 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

 
discard

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.