Forum Moderators: phranque

Message Too Old, No Replies

How to do a rewritecond on 404

404 redirect with conditional

         

alantyss

6:56 pm on Mar 26, 2008 (gmt 0)

10+ Year Member



Hi,

I am trying to redirect all images on my site that will return a 404 not found error to a generic not found image. I would also like to parse the size out. For example if someone visits my site and accesses an image http://example.com/image/albert/abc_74x74.jpg and it does not exist then I would like it to redirect to http://example.com/images/unknown_74x74.jpg

If it is a *_125x60.jpg and not found then it should map to http://example.com/images/unknown_125x60.jpg etc.

I got the RewriteRule part of it to work but not sure how to put the [OR] statement in for the other image sizes. I also cannot get the RewriteCond to work. For the condition I would like it to say 'if the url is a 404 not found'.

Here is what I have so far....

RewriteCond %{REQUEST_URI} !-U
RewriteRule ^(.+)_74x74\.jpg$ http://example.com/images/unknown_74x74.jpg [NC,L]

Any suggestions or help? Thanks a bunch.

Alantyss

[edited by: jdMorgan at 4:10 pm (utc) on Mar. 27, 2008]
[edit reason] example.com [/edit]

jdMorgan

3:46 pm on Mar 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In example.com/.htaccess:

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

This redirects any URL of the form example.com/image/<anything>/<anything>_<numbers>x<numbers>.jpg to example.com/images/unknown_<numbers>x<numbers>.jpg if the requested URL does not resolve to an existing file.

If the code is to be located in the "/image" directory, then remove that part of the path from the RewriteRule pattern, but leave "/images" in the substitution URL.

Consider carefully the fact that you will no longer get 404 error entries in your logs for missing images, and will instead have to track down missing images by looking for these redirects in your logs. Depending on the size of your site, this may significantly increase the work needed to maintain a quality site.

Jim

alantyss

3:07 pm on Mar 28, 2008 (gmt 0)

10+ Year Member



Hi Jim,

Actually, it is not working. Seems like the
RewriteCond %{REQUEST_FILENAME} !-f
is true for all images. So all the images are redirected to the default images.

I need for only the images that would return a 'file not found' to be true.

Alantyss