We help IT Professionals succeed at work.

mod_rewrite: redirect all all .ico requests

hankknight
hankknight asked
on
Using Apache and .htaccess, I want to 301 redirect ALL requests ending in .ico to http://www.example.com/favicon.ico

All these should be redirected:
http://www.example.com/adfadsfdasf.ico
http://www.example.com/ad/fad/sfd/a/sf.ico
http://www.example.com/adasdf/adsf/asdf/asdf/adsf/fadsfdasf.ico

However this should NOT be redirected to itself:
http://www.example.com/favicon.ico
Comment
Watch Question

Commented:
This should do it:
RewriteCond   %{REQUEST_URI}  !^/favicon.ico
RewriteRule  ^/(.*).ico      /favicon.ico [R=301,L]

Open in new window

CERTIFIED EXPERT
Commented:
tkuther's ruleset is close, but doesn't work for "http://www.example.com/ad/fad/sfd/a/sf.ico" scenario.

Instead, use this:


RewriteCond %{REQUEST_URI}  !^/favicon.ico
RewriteRule \.ico$  http://www.example.com/favicon.ico [R=301,L]

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.