通过RewriteRule返回时未缓存文件-Apache

时间:2019-03-14 15:06:21

标签: php apache

我已经在.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 没问题,如果我将其更改为以后的日期,文件将保持未缓存状态。

1 个答案:

答案 0 :(得分:0)

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