【问题标题】:Turn OFF Cache for specific file with Apache使用 Apache 关闭特定文件的缓存
【发布时间】:2015-09-27 18:07:12
【问题描述】:

我在一个使用 random() twig 的网站上有一个页面,在 Firefox 和 Chrome 中它被阻止工作,因为它在页面加载后立即被缓存。

有没有办法通过 Apache 配置关闭特定文件的缓存,我们称之为 default.html 或者更好的是关闭该文件的脚本部分的缓存但继续缓存图像文件?

我试过.htaccess,但这不起作用。

目前允许脚本工作的唯一方法是通过 PHP 标头全局关闭缓存:

<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
header('Pragma: no-cache');
?>

但由于我只需要为单个页面关闭缓存,因此对所有内容都关闭它似乎很疯狂。

【问题讨论】:

    标签: apache .htaccess caching


    【解决方案1】:

    想通了,要针对特定​​文件(在本例中为 index.php),将此代码添加到 .htaccess 的底部

    <Files index.php>
    FileETag None
    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, 11 Jan 1984 05:00:00 GMT"
    </Files>
    

    或者以特定文件选择为目标,即。我想缓存图像,但没有别的(匹配html, htm, js, css, php 的文件不会被缓存):

    <filesMatch "\.(html|htm|js|css|php)$">
    FileETag None
    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, 11 Jan 1984 05:00:00 GMT"
    </filesMatch>
    

    为了检查 .htaccess 是否被读取,我在底部输入了几行垃圾,发现它没有被读取,将其从 htaccess 重命名为 .htaccess 并且有效。

    【讨论】:

    • 在特定页面上调用的图像怎么样?他们还会缓存吗?
    • 我们可以用逗号分隔两个文件吗?
    猜你喜欢
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多