Search code examples
iiscachinghttp-headersfavicon

How can I get IIS to publish cache headers for my favicon.ico?


Our site's favicon.ico file hasn't changed in years - and isn't going to change any time soon - so I'd like to configure IIS to serve it with a far-future cache expiry header. The rest of the content in the root folder of my site is dynamic pages that shouldn't be cached - but as far as I can tell, IIS only allows you to set content expiry cache on a per-folder basis - so is there any way to set a cache header for favicon.ico without affecting the rest of the content in the home directory as well?


Solution

  • Yep - drill down to the file.

    In IIS Manager (on IIS6), navigate to the file, right click, properties, HTTP Headers tab, check Enable Content Expiration; party on with the other settings.

    Kudos to Dylan (below) for IIS7 instructions and this snippet.

    This creates the following section in web.config, so you can probably achieve the same thing just by editing the file directly.

    <configuration>
      <location path="favicon.ico">
        <system.webServer>
          <staticContent>
            <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="90.00:00:00" />
          </staticContent>
        </system.webServer>
      </location>
    </configuration>