[ACCEPTED]-Replace invalid image url with 404 image-http-status-code-404

Accepted answer
Score: 46

Here's one potential answer:

RewriteEngine on
RewriteRule \.(gif|jpe?g|png|bmp) /path/to/logo.gif [NC,L]

Another is to 12 use a custom scripted page:

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


Score: 24
<FilesMatch ".(jpg|png|gif)$">
ErrorDocument 404 "/path/to/image.jpg"


Score: 20

With Apache, you can have multiple .htaccess files. So, if all 5 of your images are stored in the same directory, create 4 an .htaccess file inside of that directory and add

ErrorDocument 404 /img/notfound.jpg

This 3 will create a custom 404 redirect that is 2 applied only to your image directory, plus 1 its subdirectories.

Score: 6

You can use ErrorDocument with FilesMatch 4 directive

<filesMatch "\.(jpg|png|gif)$">
ErrorDocument 404 /image.jpg 

This will show /image.jpg if a 404 image 3 uri with jpg png or gif extension is requested.

you 2 can also add your custom image or html markup 1 to the errordocument :

<filesMatch "\.(jpg|png|gif)$">
ErrorDocument 404 '<img src="image.jpg">'
Score: 2

According and in addition to AdamH's answer, you 1 should output 404 header. Here's what I'm using,


RewriteEngine on
RewriteRule \.(gif|jpe?g|png|bmp) /upload/image404.php [NC,L]


    $file = 'image404.jpg';
    $type = 'image/jpeg';
    header("HTTP/1.0 404 Not Found");
    header('Content-Length: ' . filesize($file));

More Related questions