Tuesday, July 20, 2010

Apache: favicon.ico does not exist

SkyHi @ Tuesday, July 20, 2010

If you run an Apache server then you have no doubt seen the following error message a thousand times in your error.log file.


File does not exist: /var/www/favicon.ico


The reason? You have not created a favicon, also known as a website icon, for your website. It’s the icon that displays in the address bar of your web browser when you connect to a website. A web browser will request this icon file from every website.


You have two choices. You can create a website icon for each of your websites, or you tell Apache not to log that event as an error message.


If you choose to create one. Use a program such as Gimp and create a 16×16 pixel image and save it as a .ico filetype. Then upload that file to the DocumentRoot of your website. You will need one for each VirtualHost. If you don’t have Gimp, there are online resources such as favicon.cc where you can create a .ico file and download it for your own use.


As you know by now, not having a favicon.ico file, won’t stop web browsers from requesting it each time. But you can tell Apache not to log the event as an error message. You will still see the request in the access.log, but at least you will have a cleaner error.log file.


Add the following block of code to each VirtualHost, or at least the ones which don’t have a favicon file.


Redirect 404 /favicon.ico
<Location /favicon.ico>
ErrorDocument 404 "No favicon"
</Location>

Don’t forget to restart apache after making the change. If you want make a “global” change, which would apply to any and all VirtualHosts, you can create a file in Apache’s conf.d folder with a name such as nofavicon.conf and then add that block of code to the file. That would disable favicon across the board and save you from having to edit each VirtualHost.



=============================================================

Most of my sites don't have favicons. I think it's nice to have one, but designing an icon that looks good and matches a site theme takes time and effort better spent elsewhere. My pixel-pushing powers are not at a level where making one is easy and I'd rather have the default icon than an ugly one, thank you very much.When Internet Explorer introduced favicons, it regrettably used fixed location probing to detect the presence of page icons. Other browsers have since adopted the feature, and if my server logs are any indication Firefox is even more obsessive-compulsive about them than IE. Nearly every browser visit will be associated with requests for favicon.ico, which translates into a lot of requests on a busy site.What does this mean for sites without favicons? For each and every one of those requests Apache will look for the file favicon.ico, find that it still does not exist, and return a 404 Not Found error page. If you use custom error documents (a user-friendly thing to do), that page could very well be several kilobytes in size and will not be seen by the visitor. A further annoyance is that the aforementioned failed file-finding attempt will clutter up the error log with reams of "File does not exist" errors.You can get rid of the unnecessary processing and traffic as well as the error log entries by using the following Apache configuration incantations: # Don't bother looking for favicon.ico Redirect 404 /favicon.ico # Don't bother sending the custom error page for favicon.ico ErrorDocument 404 "No favicon The Redirect directive causes Apache to immediately generate a 404 error when /favicon.ico is requested, thereby avoiding the stat calls and the error log entries caused by their inevitable failure. Whatever error document would normally be used for these 404 error response is overridden with a brief text message in the Location block. Unlike the commonly suggested workaround of creating a dummy favicon.ico file, this technique will return the appropriate HTTP status code rather than pretending to send an icon. I don't know of any browsers that will choke on a 0-byte favicon.ico, but lying to the client is bad form – at least when there's no good reason to do so.Requests for favicon.ico will still be logged in your access log; you could filter them out with conditional logging if so desired (I prefer to keep them in the log). These Apache directives will work fine in VirtualHost blocks, so the Redirect can be applied only for those sites that lack favicons without affecting the functionality of other sites.


REFERENCES

http://www.trilithium.com/johan/2005/02/no-favicon/

http://nfolamp.wordpress.com/2010/06/28/apache-favicon-ico-does-not-exist-yes-its-truly-annoying/