Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS - setting cache-control header per file type

Tags:

iis

I'd like to set different cache-control header values for my js,css and html files. I know about the option to set it on a per-folder basis but my app has html and js files in the same folder.

Is it even possible in IIS ?

like image 561
user49126 Avatar asked Oct 07 '15 08:10

user49126


2 Answers

This is possible in IIS 7+ using IIS outbound rewrite rules. Eg. if you want to invalidate all .html pages, create the following outbound rule (after installing the IIS rewrite module) in the outboundRules section of the web.config:

<outboundRules>
  <rule name="AdjustCacheForHTMLPages" preCondition="IsHTMLFile">
    <match serverVariable="RESPONSE_Cache-Control" pattern=".*" />
    <action type="Rewrite" value="no-cache, no-store, must-revalidate" />
  </rule>
  <preConditions>
    <preCondition name="IsHTMLFile">
      <add input="{REQUEST_FILENAME}" pattern=".*\.html" />
    </preCondition>
  </preConditions>
</outboundRules>
like image 103
jotap Avatar answered Sep 28 '22 05:09

jotap


The answer by @jotap will only work for file requests that end in ".html". I needed it to work for the content type, too:

<outboundRules>
  <rule name="AdjustCacheForHTMLPages" preCondition="IsHTML">
    <match serverVariable="RESPONSE_CACHE-CONTROL" pattern=".*" />
    <action type="Rewrite" value="no-cache, no-store, must-revalidate" />
  </rule>
  <preConditions>
    <preCondition name="IsHTML" logicalGrouping="MatchAny">
      <add input="{REQUEST_FILENAME}" pattern="\.html$" />
      <add input="{RESPONSE_CONTENT-TYPE}" pattern="^text/html" />
    </preCondition>
  </preConditions>
</outboundRules>
like image 33
jzsf-sz Avatar answered Sep 28 '22 05:09

jzsf-sz