For whatever crazy reason, perhaps evil-doing site scanners, requets to a web server for a favicon in all known crevasses of the site are fairly common. Since that file probably only actually exists in the root directory of your site, these requests result in a 404. If you server up a fancy, user-friendly 404 page, this can add up to a ton of bandwidth for no good reason.
This code will make those requests serve up the real favicon instead, saving bandwidth:
# REDIRECT FAVICON.ICO
<ifmodule mod_rewrite.c>
RewriteCond %{REQUEST_URI} !^/favicon\.ico [NC]
RewriteCond %{REQUEST_URI} favicon\.ico [NC]
RewriteRule (.*) https://css-tricks.com/favicon.ico [R=301,L]
</ifmodule>
Another common one is requests for a file called ajax-loader.gif, probably evil scanning looking for poorly made ajax applications in which to exploit. Make sure that file really does exist and force all requets for it to that real location.
# REDIRECT AJAX-LOADER
<ifmodule mod_rewrite.c>
RewriteCond %{REQUEST_URI} !^/images/ajax\-loader\.gif [NC]
RewriteCond %{REQUEST_URI} ajax\-loader\.gif [NC]
RewriteRule (.*) https://css-tricks.com/images/ajax-loader.gif [R=301,L]
</ifmodule>
An alternative is to just die() out if your 404 page detects the request is for ‘favicon.ico’.
Something very beautiful Thank you for offering distinctive .
Hi Chris!
Thanks very much for this snippet. I was able to solve that YSlow bug for favicon because of this.
Thanks a lot!