homepage Welcome to WebmasterWorld Guest from 184.73.104.82
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Redirect to folder if image file not found
jamesMP



 
Msg#: 4611985 posted 11:02 am on Sep 23, 2013 (gmt 0)

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

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4611985 posted 3:40 pm on Sep 23, 2013 (gmt 0)

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

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4611985 posted 4:32 pm on Sep 23, 2013 (gmt 0)

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



 
Msg#: 4611985 posted 8:54 am on Sep 24, 2013 (gmt 0)

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

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4611985 posted 10:22 am on Sep 24, 2013 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved