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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。