Search code examples
phphtmlfavicon

PHP - Should I add a favicon.ico to avoid a 404 error?


I read that the browser is looking automatically for a favicon.ico - is that also the case in an Android WebView element? Should I include a favicon.ico to a PHP page like below, to avoid a 404 error in my logs?

<?
//PHP CODE...
?>
<!DOCTYPE html>
<head>
<title>Hello world</title>
</head>
<body>
<--
Content...
-->
</body>
</html>

Solution

  • This happens because (almost every) browser searches for a favicon by default. If the accessed page don't inform a valid URL for it, Apache uses the root directory.

    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.

    1).

    If you choose the second option, add the following 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>
    

    2).

    There is no way to stop browsers requesting it. You can either create a favicon, or create a zero byte file called favicon.ico and place in the web root.