如何覆盖htaccees文件以用于缓存控制标头
在我的公司中,我们有没有启用Web缓存的HTACCESS文件,我想启用一个单个API的缓存,但是HTACCESS文件正在超越我通过标头函数设置的缓存控制,请有人帮助我吗?
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');
In my company we have htaccess file in which there is no web caching enabled,I want to enable caching for one single api but htaccess file is overriding my cache control that I am setting via header function.Can someone help me please?
htaccess file
<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)quot;>
Header set Cache-Control "max-age=86400, public"
</filesMatch>
<filesMatch "\.(css)quot;>
Header set Cache-Control "max-age=7200, public"
</filesMatch>
<filesMatch "\.(js)quot;>
Header set Cache-Control "max-age=7200, public"
</filesMatch>
<filesMatch "\.(x?html?|php)quot;>
Header set Cache-Control "private, no-store, no-cache, must-revalidate, max-age=0"
Header set Pragma "no-cache"
</filesMatch>
# END Cache-Control Headers
my api php file
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');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Apache中有一个属性setifempty,适用于版本2.2,但对我来说是2.2,所以我用以下命令替换了
上述代码,对我来说已经有效了
There is a property setifempty in apache which works for version>2.2,but for me it was 2.2 so I replaced setifempty with below command
The above code was edited and it has worked out for me