favicon.ico files and WordPress

We host some pretty high-volume WordPress sites, and one of the questions that occasionally comes up is “How can I make WordPress faster?”. That’s really just another way of saying “What part of my WordPress site is slow?”, which translates to “What requests are using a lot of CPU time?”

This question is surprisingly difficult to answer, particularly because we encourage customers who run busy WordPress sites to use FastCGI and caching. A single FastCGI process can handle lots of different PHP requests, so it’s hard to break down which individual request used what amount of server resources.

To solve this problem, we recently patched our version of PHP to optionally log the CPU time used by each request, even under FastCGI, so we could see what was really happening (patch available here).

What we found was unexpected. On some busy WordPress sites, 20–30% of the CPU time was being used to handle requests for “favicon.ico”. What the deuce?!

Looking into it, though, this makes perfect sense. The sites in question didn’t actually have a “favicon.ico” file, and WordPress has a feature that displays a custom error page for any missing file. Generating that custom page can be CPU intensive, because WordPress searches all the text of all the posts to find pages that mention “favicon.ico”.

So every time a browser requests a missing “favicon.ico” file on a WordPress site (which is very often), WordPress runs lots of time-consuming PHP and database code to try and find matching posts that mention “favicon.ico”, so it can send that back to the browser. But of course, that’s useless — WordPress is sending an HTML page to a browser that’s expecting an icon, and the browser will just ignore it. The work WordPress does is completely wasted.

The best way to solve this would be for WordPress to avoid handling requests for favicon.ico files, with something like this in the default .htaccess rules:

RewriteCond %{REQUEST_URI} !^/favicon.ico$

We’ve suggested this change to the WordPress folks. But in the meantime, we’ve also made a change to our Apache Web server setup that performs a similar check systemwide. If an HTTP request is for “/favicon.ico”, and that file doesn’t exist, the server returns an immediate 404 error instead of allowing a .htaccess file to rewrite the request to index.php or another file.

This small change has sped up some of our customers’ busiest WordPress sites by 20% or more. It shouldn’t cause any problems, because it’s hard to imagine that someone is intentionally using mod_rewrite to handle favicon.ico files. But of course, don’t hesitate to contact us if you see any unexpected favicon.ico behavior.

Update October 14 2016: The same problem now happens with files named “apple-touch-icon.png”, “apple-touch-icon-precomposed.png”, etc. that are requested by iPhones. Because of that, we’ve made the same change for these files.

3 Comments

  1. There’s a lot of helpful information in this blog post. Thank you for sharing it.
    I’ve just sent you an email to switch on Fast CGI for me. Much appreciated.

    I had noted problems with the favicon file before, in my 404 reports, and added a favicon to eliminate most of the problems. Sometimes a request for one still gets appended to the end of a post, though, such as http://www.keenerliving.com/post-name/favicon.ico This does not happen a lot, and I have not been able to figure why it happens, unless it is a WordPress fluke or something a spammer is doing. (I have not really studied my log files for it, since it is not a big problem, but that could be revealing I suppose.)

    Again, thanks for sharing all the helpful information. You folks do absolutely great work and I am always impressed with your service, both in promptness and quality, and in courtesy as well!

  2. My traffic almost from favicon.ico,,, but I have not

  3. The post is really a great helpful for everyone looking for knowledge on how to add favicon on wordpress. I enjoyed and get helped. Thanks so much.
    Favigen is a Favicon Generator which will allow you to create a favicon for your website.