doula2426 2019-03-14 15:06
浏览 68
已采纳

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

I have made RewriteRule in .htaccess to return file only if user is logged in:

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

authorize.php looks like this:

<?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.

  • 写回答

1条回答 默认 最新

  • doukuanyong1939 2019-03-15 08:48
    关注

    The problem was in Pragma: no-cache header.
    Solution was to use session_cache_limiter('private') before session_start() and add header Cache-Control: max-age=<age_in_seconds>.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?