IIS 7: Expires header for specific files

cachehttp-headersiis-7

I have this configuration for all static content on the site in the root directory:

<configuration>
<system.webServer>
   <staticContent>
     <clientCache cacheControlCustom="public"    cacheControlMode="UseExpires"
        httpExpires="Tue, 19 Jan 2038 03:14:07 GMT" />
  </staticContent>
</system.webServer>
</configuration>

Now I want to give a different expires header to a specific file in a subdirectory and to the favicon file in the root directory. I'm trying this configuration after the above code but I get a server error.

<location path="">
<system.webServer>
  <staticContent>
     <clientCache cacheControlCustom="public"    cacheControlMode="UseExpires"
        httpExpires="Tue, 19 Jan 2012 03:14:07 GMT" />
  </staticContent>
  </system.webServer>
</location>

Maybe can anyone help?

Best Answer

I don't think you can control the cache headers on a file level, only on a folder level. But you can always place the favicon in it's own folder and then you can control the cache control headers. If you want to support older browsers that always read the favicon from the root you can add a rewrite rule to rewrite the request to the specific folder.

To add the cache control headers on a per folder level you don't add the <location> tag but simply put a web.config file with the cache control tags in that specific folder.