Warm tip: This article is reproduced from stackoverflow.com, please click
.htaccess asp-classic browser-cache cache-control web-config

How can i convert this .htaccess leverage browser caching code into web.config leverage browser cach

发布于 2020-03-27 10:18:06

i have a website on a Windows Server with Microsoft IIS 8.5, i want to use the "Leverage browser caching" but i only have this .htaccess code:

<IfModule mod_expires.c>
  ExpiresActive On

  # Images
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/gif "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/webp "access plus 1 year"
  ExpiresByType image/svg+xml "access plus 1 year"
  ExpiresByType image/x-icon "access plus 1 year"

  # Video
  ExpiresByType video/mp4 "access plus 1 year"
  ExpiresByType video/mpeg "access plus 1 year"

  # CSS, JavaScript
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType text/javascript "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"

  # Others
  ExpiresByType application/pdf "access plus 1 month"
  ExpiresByType application/x-shockwave-flash "access plus 1 month"
</IfModule>

Can someone help me to transform this code into Web.config code?

P.S.: i can not use the utility of IIS in my server that automatic convert .htaccess code.

Questioner
Alessio Puccini
Viewed
88
Ralpharama 2019-07-03 21:35

I'm not sure you can be so fine-grained for client caching with web.config, you can set a blanket figure:

<system.webServer>
    <staticContent>
        <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00" />
    </staticContent>
</system.webServer>

And if you want to fine-grain it you can stop caching on certain files:

<configuration>
  <location path="path/to/filename.type">
    <system.webServer>
      <staticContent>
        <clientCache cacheControlMode="DisableCache" />
      </staticContent>
    </system.webServer>
  </location>
</configuration>

You can also use server-side Output Caching if that's something useful for you, like this:

<caching>
    <profiles>
        <add extension=".png" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="02:00:00" />
        <add extension=".woff2" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
        <add extension=".woff" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
        <add extension=".svg" policy="CacheForTimePeriod" kernelCachePolicy="CacheForTimePeriod" duration="23:59:59" />
        <add extension=".js" policy="CacheForTimePeriod" kernelCachePolicy="CacheForTimePeriod" duration="02:00:00" />
        <add extension=".jpg" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="00:10:00" />
        <add extension=".css" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="23:59:59" />
    </profiles>
</caching>

Not sure if that helps!