如何覆盖htaccees文件以用于缓存控制标头

发布于 2025-01-28 07:28:43 字数 1362 浏览 5 评论 0原文

在我的公司中,我们有没有启用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');

reverse

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');

response

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

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

发布评论

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

评论(1

匿名。 2025-02-04 07:28:43

Apache中有一个属性setifempty,适用于版本2.2,但对我来说是2.2,所以我用以下命令替换了

    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"

上述代码,对我来说已经有效了

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

    Header append Cache-Control ""
    Header edit Cache-Control "^
quot; "private, no-store, no-cache, must-revalidate, max-age=0"
    Header append Pragma ""
    Header edit Pragma "^
quot; "no-cache"

The above code was edited and it has worked out for me

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文