为特定图像添加过期标头

发布于 2024-08-26 05:39:39 字数 265 浏览 2 评论 0原文

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

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

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

All of the expires headers articles I've looked at give more or less the following solution:

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

But it doesn't make sense to me because I know which of my images are going to change and which aren't, so I want to be able to add specific expiration dates to specific image files. How would I go about this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

友欢 2024-09-02 05:39:39

您可以使用 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>

You can use a FilesMatch, eg.

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

Or for some specific files:

<FilesMatch "^(example.js|sample.css)$">
  ExpiresActive on 
  ExpiresDefault "access plus 1 month"
</FilesMatch>
冰魂雪魄 2024-09-02 05:39:39

请注意,如果您已经使用了 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>

Note that using ExpiresDefault for specific files will not work if you already used ExpiresByType. You need to use ExpiresByType again.

So this will NOT work (service-worker.js would still have +1 year expiry):

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

But this will work (service-worker.js will have +0 seconds expiry):

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

You might also use Header unset Expires. This would remove Expires header no matter what was set above it. You should also modify (or remove) Cache-Control header. It seems that mod_expires sets both.

    <FilesMatch "^(service-worker.js)$">
        Header unset Expires
        Header set Cache-Control "max-age=0"
    </FilesMatch>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文