Redirecting favicon.ico automatically

One of the overlooked web urls is the favicon.ico. To those unfamiliar, it’s the url that all web browsers and crawlers automatically try to access when viewing a website, and it’s the icon that is used for that website in user interfaces or in references to its content.

While it’s possible to tell browsers and crawlers to use a different url for a webpage’s icon using html meta tags, such as the link element in the head of a document, the favicon.ico is still used before the client knows of the correct icon url. If the favicon.ico resource does not exist, the client would receive a 404 not found error for this automatic process.

That may not seem like a problem since the client would eventually be made aware of the correct url, but consider when accessing a document that does not specify the icon url in its head, or the requested resource isn’t even html. Or perhaps the website designer made an icon that isn’t in the .ico format.

Using Nginx, it is possible to redirect the automatic requests of favicon.ico to the correct icon url. And if the html specifies its own icon, the client will still use that. The following code will do just that, anywhere, anytime.

location = /favicon.ico {
return 302 $scheme://$host/favicon.png$is_args$args;

In the above, an assumption has been made that the true icon exists as favicon.png, instead of in .ico format. When adding this to Nginx, be sure to change that to the correct url of the icon.

With the above in place, now web browsers and crawlers in search of a website’s icon will be able to find it, even if it doesn’t exist at favicon.ico, because they will follow the redirect to the correct place. And any document that specifies its own icon will still keep its icon.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.