简体   繁体   English

通过RewriteRule - apache返回时,文件不会被缓存

[英]files are not being cached when returned via a RewriteRule - apache

I have made RewriteRule in .htaccess to return file only if user is logged in: 我已经在.htaccess中创建了RewriteRule以仅当用户登录时才返回文件:

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

authorize.php looks like this: 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);

}

Here are headers from one of the files responses: 以下是其中一个文件响应的标题:

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

The problem is that all files are not being cached by a browser after these changes. 问题是这些更改之后,浏览器并未缓存所有文件。 Any ideas what could be wrong? 什么想法可能是错的?

Expires: Thu, 19 Nov 1981 08:52:00 GMT is not a problem, if I change it to a later date, files remain uncached. Expires:Thu,1981年11月19日08:52:00 GMT不是问题,如果我将其更改为以后的日期,文件仍然是未缓存的。

The problem was in Pragma: no-cache header. 问题出在Pragma: no-cache头。
Solution was to use session_cache_limiter('private') before session_start() and add header Cache-Control: max-age=<age_in_seconds> . 解决方案是使用session_cache_limiter('private')之前session_start()并添加报头Cache-Control: max-age=<age_in_seconds>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM