.htaccess: Redirect missing images from specific folder

Hi All

I’m looking for a way to redirect missing images to an image-unavailable.gif, but only if the requested image was in a specific folder. I’ve found this:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} \\.(jpg|png)$ [NC]
RewriteRule .* /images/site/image-unavailable.gif

… which seems to work for directing all missing images, but how might I change this to only act on missing images in, say, /images/myimages/ ?

Many thanks in advance,

Frank

Ah - just realised something obvious: If I put this in a .htaccess file IN /images/myimages/, that will do it. But if anyone knows how to do it from .htaccess in the root directory that would still be good to know - nice to have everything in one place…

fyankai,

For one, remove the RewriteBase which is doing nothing but messing up your code.

Secondly, WHY are you testing that the jpg/png file does NOT exist to redirect?

Regards,

DK

Thanks for your reply. As you probably guessed, I don’t really understand htaccess… I just found that code on the net and it seemed to work.

I assume though that I do want to test that the jpg/png file does NOT exist, and if it doesn’t, redirect to /images/site/image-unavailable.gif. The purpose of this script is to jump in when the page asks for an image file in directory X that doesn’t exist, and then serve up my image-unavailable.gif instead.

Well, that last bit is a reasonable “specification:”

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^X/.+\\.(png|jpg)$ images/site/image-unavailable.gif [L]

If the requested file doesn’t exist AND the request is for a file in the X subdirectory with an extension of .png or .jpg, send the unavailable image.

Regards,

DK

Thanks for taking the time to do that - much appreciated.

Yeah I should really brush up on htaccess - having to use it more and more and generally copying other people’s code…