Forum Moderators: phranque

Message Too Old, No Replies

File images are not files?

trying to put a condition for images not found

         

alantyss

1:26 am on Apr 1, 2008 (gmt 0)

10+ Year Member



I have the following piece of code

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^image/[^/]+/[^_]+_([0-9]+x[0-9]+)\.jpg$ http://example.com/images/unknown_$1.jpg [R=301,L]

The RewriteCond %{REQUEST_FILENAME} !-f is true for all images. I would like the code to find all images that are not found. By placing the above code it thinks that all images are not files even though they exist. Can someone help?

Thanks!

lammert

9:47 am on Apr 1, 2008 (gmt 0)

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



Why not just the following in your httpd.conf file?

<Directory "/your-site-path/images">
ErrorDocument 404 /images/not-here.jpg
</Directory>

You could place the ErrorDocument statement also in an .htaccess file in your images directory.

alantyss

12:42 pm on Apr 1, 2008 (gmt 0)

10+ Year Member



I have images of different sizes. So I would like to map abc_74x74.jpg if not found to unknown_74x74.jpg and etc for the other sizes. Is there a way to make the code you recommended dynamic?

Thanks!

jdMorgan

1:21 pm on Apr 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Testing "file exists" and "directory exists" checks is always problematic because it's a "silent" test -- Either it works properly or it doesn't.

In these cases, I suggest temporarily changing the code so you can see what is going on:


# Commented-out for testing
# RewriteCond %{REQUEST_FILENAME} !-f
#
# Prevent looping while doing this test
RewriteCond %{REQUEST_URI} !^images/unknown_
# Rewrite image requests to "/images/unknown_" and show REQUEST_FILENAME in address bar
RewriteRule ^image/[^/]+/[^_]+_([0-9]+x[0-9]+)\.jpg$ http://example.com/images/unknown_$1.jpg?req_filepath=%{REQUEST_FILENAME} [R=301,L]

Now request an image, and look at the req_filepath value in the query string in your address bar. What is wrong with that path? It is most likely that it's wrong because either your server adds in some additional path info (see mod_rewrite RewriteBase directive) or because you've got another rewrite that changes the URL-path before (or after) this code runs. Or it's possible that the "/images/unknown_<image>.jpg" path isn't being properly constructed to point to an existing file.

At any rate, looking at the actual filepath that is being tested --along with the info in your server error log-- will tell you.

Jim

alantyss

3:36 pm on Apr 1, 2008 (gmt 0)

10+ Year Member



Thanks Jim!

You are right. There was a conflict with the document root and the actual path of the image file. Had to play around with it but got it to work. Thanks a million!

alantyss