Forum Moderators: phranque

Message Too Old, No Replies

Redirect to folder if image file not found

         

jamesMP

11:02 am on Sep 23, 2013 (gmt 0)

10+ Year Member



How would I create a RewriteCondition which matches only images files and if they're not found, tries a subfolder, a sub-subfolder, and then if still not found returns a 404?

For example:
If
example.com/images/image1.jpg
does not exist try
example.com/images/folder/image1.jpg

and:
If
example.com/image1.jpg
does not exist try
example.com/images/image1.jpg
and then
example.com/images/folder/image1.jpg


I've come up with the following, but any variation I try either gives a server error or looks in
/folder/folder/..(repeats)../folder/images/image1.jpg


RewriteCond %{REQUEST_FILENAME} \.(png|bmp|jpeg|jpg|gif)$ !-f
RewriteRule ^(.*) /images/$1 [R]
RewriteRule ^(.*) /images/folder/$1 [R,L]


Thanks for any advice.

JD_Toims

3:40 pm on Sep 23, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



How would I create a RewriteCondition which matches only images files and if they're not found, tries a subfolder, a sub-subfolder, and then if still not found returns a 404?

Rewrite to PHP and use is_file() + file_get_contents() instead.

lucy24

4:32 pm on Sep 23, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Within mod_rewrite, you would need to use the dreaded

!-f

flag in a RewriteCond. Don't put the Filename part in a Condition; that should be in the body of the rule.

But it's better if you post back with more detail about the exact circumstances, including how many potential files are involved.

jamesMP

8:54 am on Sep 24, 2013 (gmt 0)

10+ Year Member



Its basically to support an image heavy legacy site that was hosted on the same domain. The previous webmaster saw fit to save *everything* at root level, with the exception of approx 250 images (in 'images' folder), so we're talking 500(ish) images in total.

So, my aim is to move all the image files into /images/legacy/ to keep the directory structure a bit more tidy whilst still being able to serve them from the original reference.

phranque

10:22 am on Sep 24, 2013 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



move all images to one directory and internally rewrite all image requests to that directory.
anything that's missing will return a 404.