How to prevent favicon.ico requests?

前端 未结 11 921
栀梦
栀梦 2020-11-22 10:34

I don\'t have a favicon.ico, but my browser always makes a request for it.

Is it possible to prevent the browser from making a request for the favicon from my site? M

相关标签:
11条回答
  • 2020-11-22 10:35

    if you use nginx

    # skip favicon.ico
    #
    location = /favicon.ico {
        access_log off;
        return 204;
    }
    
    0 讨论(0)
  • 2020-11-22 10:37

    You can't. All you can do is to make that image as small as possible and set some cache invalidation headers (Expires, Cache-Control) far in the future. Here's what Yahoo! has to say about favicon.ico requests.

    0 讨论(0)
  • 2020-11-22 10:38

    A very simple solution is put the below code in your .htaccess. I had the same issue and it solve my problem.

    <IfModule mod_alias.c>
        RedirectMatch 403 favicon.ico
    </IfModule>
    

    Reference: http://perishablepress.com/block-favicon-url-404-requests/

    0 讨论(0)
  • 2020-11-22 10:40

    I will first say that having a favicon in a Web page is a good thing (normally).

    However it is not always desired and sometime developers need a way to avoid the extra payload. For example an IFRAME would request a favicon without showing it. Worst yet, in Chrome and Android an IFRAME will generate 3 requests for favicons:

    "GET /favicon.ico HTTP/1.1" 404 183
    "GET /apple-touch-icon-precomposed.png HTTP/1.1" 404 197
    "GET /apple-touch-icon.png HTTP/1.1" 404 189
    

    The following uses data URI and can be used to avoid fake favicon requests:

    <link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon"> 
    

    For references see here:

    • https://github.com/h5bp/html5-boilerplate/issues/1103
    • https://twitter.com/diegoperini/status/4882543836930048

    UPDATE 1:

    From the comments (jpic) it looks like Firefox >= 25 doesn't like the above syntax anymore. I tested on Firefox 27 and it doesn't work while it still work on Webkit/Chrome.

    So here is the new one that should cover all recent browsers. I tested Safari, Chrome and Firefox:

    <link rel="icon" href="data:;base64,=">
    

    I left out the "shortcut" name from the "rel" attribute value since that's only for older IE and versions of IE < 8 doesn't like dataURIs either. Not tested on IE8.

    UPDATE 2:

    If you need your document to validate against HTML5 use this instead:

    <link rel="icon" href="data:;base64,iVBORw0KGgo=">
    
    0 讨论(0)
  • 2020-11-22 10:41

    In our experience, with Apache falling over on request of favicon.ico, we commented out extra headers in the .htaccess file.

    For example we had Header set X-XSS-Protection "1; mode=block"

    ... but we had forgotten to sudo a2enmod headers beforehand. Commenting out extra headers being sent resolved our favicon.ico issue.

    We also had several virtual hosts set up for development, and only failed out with 500 Internal Server Error when using http://localhost and fetching /favicon.ico. If you run "curl -v http://localhost/favicon.ico" and get a warning about the host name not being in the resolver cache or something to that effect, you might experience problems.

    It could be as simple as not fetching (we tried that and it didn't work, because our root cause was different) or look around for directives in apache2.conf or .htaccess which might be causing strange 500 Internal Server Error messages.

    We found it failed so quickly there was nothing useful in Apache's error logs whatsoever and spent an entire morning changing small things here and there until we resolved the problem of setting extra headers when we had forgotten to have mod_headers loaded!

    0 讨论(0)
  • 2020-11-22 10:45

    I believe I've seen this (I haven't tested it or used it personally though):

    <link rel="shortcut icon" href="#" />
    

    Anyone had similar experience?

    EDIT:

    I just tested the above snippet and on a forced full refresh, no favicon requests were seen in Fiddler. I tested against IE8 (Compat mode as IE7 standards) and FF 3.6.

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