这应该是直截了当的,但显然不是。
我试图避免在 Apache 服务器上缓存 service-worker 文件。
该文件是 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
, 但我不。
最佳答案
FilesMatch
指令中的正则表达式显示正确。但请注意,Apache 将发送以下 header 作为响应:
Cache-Control: max-age=0, no-cache, no-store, must-revalidate
请注意此处的混合大小写 header 名称Cache-Control
。
您的 grep
命令正在搜索所有小写 header 名称,即 cache-control
,因此没有显示输出。
您需要在 grep
中使用 -i
(忽略大小写)匹配或搜索完全相同的 header ,即 Cache-Control
。
因此以下任何一个 grep
都可以工作:
grep -i 'cache-control'
或
grep 'Cache-Control'
为了提高效率,请为固定字符串搜索添加 -F
选项,因为您没有在 grep
模式中使用任何正则表达式来实现它:
grep -iF 'cache-control'
关于regex - htaccess 目标特定文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53902953/