How can I apply <clientCache /> settings to a specific extension in IIS? How can I apply <clientCache /> settings to a specific extension in IIS? azure azure

How can I apply <clientCache /> settings to a specific extension in IIS?


As others have mentioned it is not possible so I would like to suggest a workaround to do the job.
You can take advantage of the capabilities of the URL Rewrite Module by creating an outbound rule to replace Cache-Control header of the html files.

Here's the config.

<?xml version="1.0" encoding="UTF-8"?><configuration>    <system.webServer>        <staticContent>            <mimeMap fileExtension=".text" mimeType="text/plain" />            <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />        </staticContent>        <rewrite>            <outboundRules>                <rule name="RewriteCacheControlForHTMLFiles" preCondition="FileEndsWithHtml">                    <match serverVariable="RESPONSE_Cache_Control" pattern=".*" />                    <action type="Rewrite" value="max-age=86400" />                </rule>                <preConditions>                    <preCondition name="FileEndsWithHtml">                        <add input="{REQUEST_FILENAME}" pattern="\.html$" />                    </preCondition>                </preConditions>            </outboundRules>        </rewrite>    </system.webServer></configuration>

A screenshot of my test:

enter image description here


Are these files (html vs content) in separate folders?If so, you can setup a folder specific caching age

<configuration><location path="[html files path]">    <system.webServer>      <staticContent>        <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="[your requirement]" ></clientCache>      </staticContent>    </system.webServer>  </location></configuration>


Unfortunately the <location> element doesn't support regex or wildcards so you won't be able to do exactly what you are asking.

I can think of a couple of ways to achieve your goal though. The first is to write an http module that intercepts requests to images and changes the caching headers.

The other option is to leave your html unchanged but to move all your images to an images folder. You could then set a caching rule on that folder. You could use a redirect rule to redirect requests for images to that images folder. If you choose this path I can give you more information about how you might set it up.