Forum Moderators: phranque

Message Too Old, No Replies

404 images

mod_rewrite 404 images

         

ultradude25

12:22 pm on Mar 21, 2010 (gmt 0)

10+ Year Member



I use my site to host a lot of images I use to hotlink on other sites. Generally I keep all images, but sometimes something screws up or gets deleted and the hotlinked image will no longer exist, and leave a blank space which makes people confused.

I'd like to make a 404 image show up in place of the image if it is missing.

Also, it would be preferable if it still threw out a 404 error to the error log.

How would I go about doing this? And I know it is possible because I've seen other sites like Photobucket and Imageshack do it.

My setup is:
The actual images being hotlinked:
FTP: /public_html/images/
URL: http://images.example.com/

There is also a folder after that which has images taken with ZScreen
FTP: /public_html/images/ZScreen/
URL: http://images.example.com/ZScreen

The 404 image:
FTP: /public_html/images/404.png
URL: http://images.example.com/404.png

I have jpg, png and ico files on there.

[edited by: jdMorgan at 1:15 pm (utc) on Mar 21, 2010]
[edit reason] de-linked URLs for clarity [/edit]

jdMorgan

1:31 pm on Mar 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The simplest approach would probably be to use <FilesMatch> to override the existing ErrorDocument and declare an image as the errordocument for missing images only:

ErrorDocument 404 /usual-error-document.html
#
<FilesMatch "\.(gif|jpg|png)$">
ErrorDocument 404 /404.gif
</FilesMatch>

Note three things:

1) I specified a .gif as the error image, because .gif filesizes are usually smaller. I did not specify a .png as the error image, because .png support is not universal. You could probably use .jpg successfully, but this rules out using a transparent image (see #3 below).

2) The URL-path to the errordocument (or in this case, error image) *must* be a local filepath. If you specify a full URL, then the response code returned by the server will not be 404, it will be 302-Found. This behaviour is documented in the Apache core ErrorDocument directive's documentation. This may mean that your replacment image will have to be served from "example.com" rather than from "images.example.com" and if so, then so be it.

3) The image that you provide as the error image must be designed to 'scale' to the aspect ratio and size specified in the HTML <img> tags of all of the images that it is to replace. Therefore, if it is *not* simply a 1x1 transparent .gif, then it must be kept *very* simple -- Perhaps an image of a couple of very-short lines of text in a very-plain font that will be readable even if distorted to fit a different aspect ratio and scaled up or down to fit a different size. I have had best success at replacing both "portrait" and "landscape" images by simply using a "square" replacement image which, on average, gets "stretched" the least in either dimension when replacing either of the other formats.

Jim

ultradude25

7:14 am on Mar 22, 2010 (gmt 0)

10+ Year Member



Many thanks, it works perfectly.