【问题标题】:files are not being cached when returned via a RewriteRule - apache通过 RewriteRule 返回时文件没有被缓存 - apache
【发布时间】:2019-03-15 16:48:44
【问题描述】:

我在 .htaccess 中创建了 RewriteRule 以仅在用户登录时返回文件:

RewriteRule ^(.*)$ ../authorize.php?file=$1 [NC]

authorize.php 看起来像这样:

<?php
session_start();
if (!isset($_SESSION["user_id"]) || $_SESSION["user_sessid"] != session_id() || $_REQUEST["token"] != $_TOKEN) {
    header("HTTP/1.1 404 NOT FOUND");
} else {
    $file = "public/" . $_REQUEST['file'];
    $contentType = mime_content_type($file);
    header("Content-type: $contentType");
    header('Content-Disposition: inline;');
    readfile($file);

}

以下是其中一个文件响应的标题:

HTTP/1.1 200 OK
Date: Thu, 14 Mar 2019 14:51:54 GMT
Server: Apache/2.4.25 (Debian)
X-Powered-By: PHP/7.0.33
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: max-age=3153600000
Pragma: no-cache
Content-Disposition: inline;
Keep-Alive: timeout=5, max=98
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: image/jpeg

问题是在这些更改之后浏览器并未缓存所有文件。有什么想法可能是错的吗?

到期时间:1981 年 11 月 19 日星期四 08:52:00 GMT 没有问题,如果我将其更改为以后的日期,文件仍处于未缓存状态。

【问题讨论】:

    标签: php apache


    【解决方案1】:

    问题出在Pragma: no-cache 标头中。
    解决方案是在session_start() 之前使用session_cache_limiter('private') 并添加标头Cache-Control: max-age=&lt;age_in_seconds&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-14
      • 1970-01-01
      • 2015-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-03
      • 1970-01-01
      相关资源
      最近更新 更多