Replace invalid image url with 404 image

前端 未结 5 1879
生来不讨喜
生来不讨喜 2020-12-08 10:34

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 i

相关标签:
5条回答
  • 2020-12-08 10:58

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

    .htaccess

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

    image404.php

    <?php
        $file = 'image404.jpg';
        $type = 'image/jpeg';
        header("HTTP/1.0 404 Not Found");
        header('Content-Type:'.$type);
        header('Content-Length: ' . filesize($file));
        readfile($file);
    ?>
    
    0 讨论(0)
  • 2020-12-08 11:05

    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.

    0 讨论(0)
  • 2020-12-08 11:12
    <FilesMatch ".(jpg|png|gif)$">
    ErrorDocument 404 "/path/to/image.jpg"
    </FilesMatch>
    
    0 讨论(0)
  • 2020-12-08 11:12

    You can use ErrorDocument with FilesMatch directive

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

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

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

    <filesMatch "\.(jpg|png|gif)$">
    ErrorDocument 404 '<img src="image.jpg">'
    </filesMatch>
    
    0 讨论(0)
  • 2020-12-08 11:13

    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

    0 讨论(0)
提交回复
热议问题