【问题标题】:PHP configuration overriding the .htaccess caching headers覆盖 .htaccess 缓存标头的 PHP 配置
【发布时间】:2014-09-19 07:48:34
【问题描述】:

我一直在尝试为我的站点的静态文件(如 jpg、png、css 等)设置缓存,但到目前为止没有成功,我发现 PHP 的配置正在接管 .htaccess 配置。

我已确认 mod_expires.so 在我的 httpd.conf 中未注释。

我尝试在 .htaccess 中关注但徒劳无功。

<IfModule mod_expires.c>
ExpiresActive On
<FilesMatch "\.(jpe?g|png|gif|js|css|ttf|eto|svg|woff|otf)$">
ExpiresDefault "access plus 1 month"
</FilesMatch>
</IfModule>

甚至尝试过都没有成功

<FilesMatch "\.(flv|gif|jpg|jpeg|png|ico)$">
Header set Cache-Control "max-age=2592000"
</FilesMatch>

然后我发现我的 php.ini 有 session.cache_limiter = nocache 我将其更改为 session.cache_limiter = public 和我的 session.cache_expire=180 在 php.ini 中更改此配置并重新启动 apache 后,我发现图像的响应标头更改如下

HTTP/1.1 200 OK
Date: Fri, 19 Sep 2014 07:33:58 GMT
Server: Apache/2.2.3 (CentOS)
X-Powered-By: PHP/5.2.10
Expires: Fri, 19 Sep 2014 10:33:58 GMT
Cache-Control: public, max-age=10800
Last-Modified: Mon, 05 Dec 2011 19:08:22 GMT
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

我要做的是为“访问加1个月”缓存图像和css/js文件

任何建议表示赞赏。

【问题讨论】:

    标签: php apache .htaccess caching mod-expires


    【解决方案1】:

    你可以使用ExpiresByType指令:

    ExpiresActive on
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    

    【讨论】:

    • 谢谢你的回答,但我也试过了:(
    • 是的,这就是我提到的,似乎 PHP 配置正在覆盖 htaccess 指令。
    • PHP 配置无法覆盖 text/javascript 或 image/gif 的缓存。尝试注释掉您的 FilesMatch 块并在新浏览器中进行测试。这些行之前还有ExpiresActive on吗?
    • 不走运,同样的结果 :(
    • 祝您测试顺利,因为只有您拥有可用于调试的所有资源/文件。我只能保证这是有效的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    相关资源
    最近更新 更多