【问题标题】:Add Expires Headers for Specific Images为特定图像添加过期标头
【发布时间】:2010-03-24 15:08:09
【问题描述】:

我看过的所有过期标题文章都或多或少地提供了以下解决方案:

ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000

但这对我来说没有意义,因为我知道我的哪些图像会改变,哪些不会,所以我希望能够为特定的图像文件添加特定的到期日期。我该怎么办?

【问题讨论】:

    标签: .htaccess mod-expires


    【解决方案1】:

    您可以使用FilesMatch,例如。

    <FilesMatch "\.(js|css)$">
      ExpiresActive on 
      ExpiresDefault "access plus 1 month"
    </FilesMatch>
    

    或者对于一些特定的文件:

    <FilesMatch "^(example.js|sample.css)$">
      ExpiresActive on 
      ExpiresDefault "access plus 1 month"
    </FilesMatch>
    

    【讨论】:

    • 您能否举例说明如何将其应用于特定的图像文件:example.jpg?谢谢!
    • 感谢您的帮助,非常感谢:那么下面的工作是否可行? (image.jpg|anotherImage.png|backgroundImage.png|bannerImg.jpg)
    • 将此代码添加到我的 .htaccess 给我一个 500 内部服务器错误。
    • @a'r:我认为您需要转义点 &lt;FilesMatch "^(example\.js|sample\.css)$"&gt; 并且可能值得添加 Header append Cache-Control "public" 阅读更多:stackoverflow.com/a/10034368/260080
    【解决方案2】:

    请注意,如果您已经使用ExpiresByType,则对特定文件使用ExpiresDefault 将不起作用。您需要再次使用ExpiresByType

    所以这行不通(service-worker.js 仍然有 +1 年到期):

    <IfModule mod_expires.c>
        ExpiresActive on
        ExpiresDefault                                      "access plus 1 month"
        ExpiresByType application/javascript                "access plus 1 year"
        <FilesMatch "^(service-worker.js)$">
            ExpiresDefault                                  "access plus 0 seconds"
        </FilesMatch>
    </IfModule>
    

    但这会起作用(service-worker.js 将有 +0 秒到期):

    <IfModule mod_expires.c>
        ExpiresActive on
        ExpiresDefault                                      "access plus 1 month"
        ExpiresByType application/javascript                "access plus 1 year"
        <FilesMatch "^(service-worker.js)$">
            ExpiresByType application/javascript            "access plus 0 seconds"
        </FilesMatch>
    </IfModule>
    

    您也可以使用Header unset Expires。这将删除 Expires 标头,无论在其上方设置什么。您还应该修改(或删除)Cache-Control 标头。似乎mod_expires 设置了两者。

        <FilesMatch "^(service-worker.js)$">
            Header unset Expires
            Header set Cache-Control "max-age=0"
        </FilesMatch>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-08
      • 2010-12-08
      • 2011-12-10
      • 1970-01-01
      • 2011-10-08
      • 2020-11-20
      • 2016-12-06
      • 2014-07-19
      相关资源
      最近更新 更多