【问题标题】:htacces target specific filehtaccess 目标特定文件
【发布时间】:2018-12-23 10:48:45
【问题描述】:

这应该是直截了当的,但显然不是。

我正在尝试避免将 service-worker 文件缓存在 Apache 服务器上。

文件是sw.js,它应该放在/public_html/ 上。

我尝试了以下<FilesMatch ??? > 的几种组合,例如"sw\.js$",但均未成功。

<FilesMatch "^(sw\.js)$">
  FileETag None
  <ifModule mod_headers.c>
    Header unset ETag
    Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "Wed, 08 Jan 1975 05:00:00 GMT"
  </ifModule>
</FilesMatch>

根据to this,如果我运行

 `curl -I -L https://www.soeezauto.com/sw.js | grep cache-control` 

我应该收到cache-control: no-cache,但我没有。

【问题讨论】:

  • 你的 &lt;FilesMatch&gt; 指令应该匹配,但由于你只匹配一个文件,你可以简单地使用 &lt;Files "sw.js"&gt; - 这里不需要正则表达式。尝试设置自定义测试标头,以防这些缓存标头被覆盖...Header set X-TEST "test".
  • 这在我的 Apache 上运行良好。使用grep -iF 'cache-control'查看
  • @DocRoot。我也尝试过使用
  • @anubhava。它确实有效。请详细说明原因(-if 标志有什么作用)并将其作为答案发布,以便我接受。

标签: regex apache .htaccess caching service-worker


【解决方案1】:

FilesMatch 指令中的正则表达式看起来是正确的。但是请注意,Apache 将发送以下标头作为响应:

Cache-Control: max-age=0, no-cache, no-store, must-revalidate

注意这里混合大小写的标题名称Cache-Control

您的grep 命令正在搜索所有小写标题名称,即cache-control,因此没有显示输出。

您需要在grep 中使用-i(忽略大小写)匹配或搜索完全相同的标题,即Cache-Control

因此,以下任何grep 都可以使用:

grep -i 'cache-control'

grep 'Cache-Control'

为了提高效率,为固定字符串搜索添加 -F 选项,因为您没有在 grep 模式中使用任何正则表达式来实现它:

grep -iF 'cache-control'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 2011-09-11
    • 2022-10-12
    • 1970-01-01
    • 2015-12-11
    • 2020-09-14
    • 1970-01-01
    相关资源
    最近更新 更多