如何覆盖htaccees文件的缓存控制头



在我的公司中,我们有htaccess文件,其中没有启用web缓存,我想为一个api启用缓存,但htaccess文件覆盖了我通过header函数设置的缓存控制。有人能帮帮我吗?

htaccess

<ifModule mod_headers.c>
#BEGIN Security Headers
Header set X-XSS-Protection "1; mode=block"
Header set X-Frame-Options "SAMEORIGIN"
Header set X-Content-Type-Options "nosniff"
#END Security Headers
# BEGIN Cache-Control Headers
#To disable ETags
Header unset ETag
<filesMatch ".(ico|jpe?g|png|gif|swf|woff)$">
Header set Cache-Control "max-age=86400, public"
</filesMatch>
<filesMatch ".(css)$">
Header set Cache-Control "max-age=7200, public"
</filesMatch>
<filesMatch ".(js)$">
Header set Cache-Control "max-age=7200, public"
</filesMatch>
<filesMatch ".(x?html?|php)$">
Header set Cache-Control "private, no-store, no-cache, must-revalidate, max-age=0"
Header set Pragma "no-cache"
</filesMatch>
# END Cache-Control Headers

我的API PHP文件

header("Pragma: cache");
header("Cache-Control: max-age=300");
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 300) . " GMT");
header('Last-Modified: ' . gmdate("D, d M Y H:i:s", time()) . ' GMT');
header('Content-Type: application/json');

反应

在apache中有一个属性setifempty适用于版本>2.2,但对我来说它是2.2,所以我用下面的命令

替换了setifempty
Header append Cache-Control ""
Header edit Cache-Control "^$" "private, no-store, no-cache, must-revalidate, max-age=0"
Header append Pragma ""
Header edit Pragma "^$" "no-cache"

上面的代码是编辑过的,它已经为我工作了

最新更新