首页 > 解决方案 > 如何将此 .htaccess 杠杆浏览器缓存代码转换为 web.config 杠杆浏览器缓存代码?

问题描述

我在带有 Microsoft IIS 8.5 的 Windows Server 上有一个网站,我想使用“利用浏览器缓存”,但我只有这个 .htaccess 代码:

<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>

有人可以帮我将此代码转换为 Web.config 代码吗?

PS:我无法在我的服务器中使用 IIS 的实用程序来自动转换 .htaccess 代码。

标签: .htaccessasp-classicweb-configbrowser-cachecache-control

解决方案


我不确定您是否可以使用 web.config 对客户端缓存进行如此精细的处理,您可以设置一个总体数字:

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

如果你想细化它,你可以停止缓存某些文件:

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

如果这对您有用,您还可以使用服务器端输出缓存,如下所示:

<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>

不知道这是否有帮助!


推荐阅读