views:

49

answers:

2

I don't know if this is possible or not, I have an image host that I've made myself. I need some last tweaks with it.

Whenever an image has been deleted or is an invalid image url, it should replace with an 404 image, so for example if someone adds this:

http://imagehosturl.com/i/34njk5n.jpg

But it's an invalid link, so I need it to show:

http://imagehosturl.com/img/notfound.jpg

Which is like this:

alt text

I do know that .htaccess can do this with it's ErrorDocument 404, but I have one already when a user access to an invalid page, so it would show the 404 page.

So whenever a user hotlinks an image and it's invalid or is deleted, I need it to be replaced with the 404 image.

How can I make this?

+2  A: 

Here's one potential answer:

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule \.(gif|jpe?g|png|bmp) /path/to/logo.gif [NC,L]

Another is to use a custom scripted page:

Use the errorDocument directive (documented at [httpd.apache.org ]) to point a '404' error to a script (perl, PHP, whatever). If the requested file has an image extension (or has an image/* mimetype; PHP supplies the mime_content_type [us2.php.net] function for this; I'm sure there are many ways to do this in perl; the MIME::Types [search.cpan.org] module is one way), then set the "Content-Type" header to the mimetype of your logo image and return the content of the logoimage to the browser.

http://www.webmasterworld.com/forum92/3458.htm

AdamH
The first one worked, just tweaked it as said from that link, so it works perfectly. Thanks.
YouBook
+1  A: 

With Apache, you can have multiple .htaccess files. So, if all of your images are stored in the same directory, create an .htaccess file inside of that directory and add ErrorDocument 404 /img/notfound.jpg. This will create a custom 404 redirect that is applied only to your image directory, plus its subdirectories.

Garrett Hyde
This would work, didn't think of this. Accepted the answer before yours. Sorry buddy.
YouBook