5

我已经在这里回答了 100 多个答案,尝试了很多,没有任何工作?

有一个基于 PHP 的网站。我需要为所有 .php 文件缓存 OFF,除了 SELECT FEW。

因此,在 .htaccess 中,我有以下内容:

ExpiresActive On
# Eliminate caching for certain dynamic files
<FilesMatch "\.(php|cgi|pl)$">
ExpiresDefault A0
Header set Cache-Control "no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, no-transform"
Header set Pragma "no-cache"
</FilesMatch>

使用 Firebug,我看到以下内容:

Cache-Control   no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, no-transform
Connection  Keep-Alive
Content-Type    text/html
Date    Sun, 02 Sep 2012 19:22:27 GMT
Expires Sun, 02 Sep 2012 19:22:27 GMT
Keep-Alive  timeout=3, max=100
Pragma  no-cache
Server  Apache
Transfer-Encoding   chunked
X-Powered-By    PHP/5.2.17

嘿,看起来很棒!

但是,我有几个 .php 页面需要一些非常短的缓存。

我认为简单的答案是将其添加到我希望启用缓存的每个 php 页面的最顶部:

<?php header("Cache-Control: max-age=360"); ?>

没有。

然后我尝试了上述的各种版本。没有。

然后我尝试了 meta http-equiv 变体。没有。

然后我尝试了 .htaccess 代码的变体以及上述变体,例如将其限制为:

# Eliminate caching for certain dynamic files
<FilesMatch "\.(php|cgi|pl)$">
Header set Cache-Control "no-cache, max-age=0"
</FilesMatch>

没有。

似乎我所做的任何事情都不会允许使用 .htaccess 代码启用单个 .php 缓存,而不是完全从 .htaccess 文件中删除语句。

我哪里错了?我该怎么做才能使单个 php 页面可缓存而其余页面保持关闭?

谢谢你的任何想法。

4

3 回答 3

3

好吧,显然这没有答案。因此,我此时的解决方案是完全消除 .htaccess 代码,并将显式标头应用于每个文件。你知道的痛苦,但现在是继续前进的时候了。如果有人有更优雅的解决方案可以使用 .htaccess 默认值,请随时分享...谢谢

于 2012-09-10T16:50:37.973 回答
3

所以我知道我迟到了……也许为时已晚。但我遇到了一个类似的问题,我想我会分享我的解决方案。

基本上,我为每个我不想被缓存的文件(或者与我的静态资源具有不同的缓存时间)关闭了 ExpiresActive。它看起来像这样:

ExpiresActive On

<FilesMatch "\.(php|cgi|pl)$">

  # This makes sure that no cache headers can be set,
  # but does not generate an error when trying to.
  ExpiresActive Off

</FilesMatch>

# Your normal caching here
ExpiresDefault "access plus 1 month"

现在在您的 PHP 脚本中,您应该能够插入缓存标头而不会被您的 .htaccess 文件覆盖,就像您正在做的那样

<?php header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 360) . ' GMT'); ?>

希望这会有所帮助。

于 2015-05-09T23:30:46.617 回答
1

这个答案为我提供了解决方案:https ://stackoverflow.com/a/4521120/2685496

我几乎不知道session_start();默认情况下会用值覆盖您的 Cache-control 和 Expires 标头,以确保页面不被缓存。

你可以使用session_cache_limiter('public');before session_start();,就像 Marcin 建议的那样,或者你可以把你的header();陈述放在 after session_start();

于 2017-03-22T09:48:58.843 回答