Looking through my server logs, I see that a lot of pages on my site are requesting favicon.ico, favicon.jpg, favicon.png, etc in a variety of different directories.

Instead of wading through each page to try to figure out where each incorrect request is coming from, I'm writing some apache redirect rules to change a request for any url containing "favicon" to redirect to /favicon.ico

My initial naive attempt was this:

RewriteRule favicon /favicon.ico [R=301,L]

But that meant that when you actually requested /favicon.ico it would send you into an infinite redirect loop.

Basically what I think I need is a regex which has this effect:

| Request                | Response     |
|------------------------|--------------|
| favicon.png            | /favicon.ico |
| directory/favicon.png  | /favicon.ico |
| directory/favicon.ico  | /favicon.ico |
| favicon.ico            | <no match>   |

Accepted Answer

Try this

 RewriteCond %{REQUEST_URI} !^/favicon\.ico [NC] # if not already going to favicon.ico
 RewriteCond %{REQUEST_URI} favicon\.ico [NC] # edit this line to match your favicon matching regex
 RewriteRule (.*) http://www.domain.com.au/favicon.ico [R=301,L] #redirect to the real address
Written by alex
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki