How to prevent favicon.ico requests?

前端 未结 11 911
栀梦
栀梦 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:49

    You can use .htaccess or server directives to deny access to favicon.ico, but the server will send an access denied reply to the browser and this still slows page access.

    You can stop the browser requesting favicon.ico when a user returns to your site, by getting it to stay in the browser cache.

    First, provide a small favicon.ico image, could be blank, but as small as possible. I made a black and white one under 200 bytes. Then, using .htaccess or server directives, set the file Expires header a month or two in the future. When the same user comes back to your site it will be loaded from the browser cache and no request will go to your site. No more 404's in the server logs too.

    If you have control over a complete Apache server or maybe a virtual server you can do this:-

    If the server document root is say /var/www/html then add this to /etc/httpd/conf/httpd.conf:-

    Alias /favicon.ico "/var/www/html/favicon.ico"
    <Directory "/var/www/html">
        <Files favicon.ico>
           ExpiresActive On
           ExpiresDefault "access plus 1 month"
        </Files>
    </Directory>
    

    Then a single favicon.ico will work for all the virtual hosted sites since you are aliasing it. It will be drawn from the browser cache for a month after the users visit.

    For .htaccess this is reported to work (not checked by me):-

    AddType image/x-icon .ico
    ExpiresActive On
    ExpiresByType image/x-icon "access plus 1 month"
    
    0 讨论(0)
  • 2020-11-22 10:51

    Just add the following line to the <head> section of your HTML file:

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

    Features of this solution:

    • 100% valid HTML5
    • very short
    • does not incur any quirks from IE 8 and older
    • does not make the browser interpret the current HTML code as favicon (which would be the case with href="#")
    0 讨论(0)
  • 2020-11-22 10:53

    Sometimes this error comes, when HTML has some commented code and browser is trying to look for something. Like in my case I had commented code for a web form in flask and I was getting this.

    After spending 2 hours I fixed it in the following ways:

    1) I created a new python environment and then it threw an error on the commented HTML line, before this I was only thrown error 'GET /favicon.ico HTTP/1.1" 404'

    2) Sometimes, when I had a duplicate code, like python file existing with the same name, then also I saw this error, try removing those too

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

    In Node.js,

    res.writeHead(200, {'Content-Type': 'text/plain', 'Link': 'rel="shortcut icon" href="#"'} );
    
    0 讨论(0)
  • 2020-11-22 10:56

    You could use

    <link rel="shortcut icon" href="http://localhost/" />
    

    That way it won't actually be requested from the server.

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