有条件设置缓存头在Apache中 [英] Conditionally setting cache headers in apache

查看:141
本文介绍了有条件设置缓存头在Apache中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要有条件地设置根据从被访问路径是什么文件缓存头。基本上,访问 http://www.example.com/cache/$cache_key/* 应成为文件,在遥远的将来缓存头。

I want to conditionally set cache headers depending on what path files are accessed from. Basically, accessing http://www.example.com/cache/$cache_key/* should serve files with far in the future cache headers.

我使用重写规则来设置环境变量,然后尝试设置基于变量缓存控制头。但是,好像被设置变量来不及在这个过程什么的;有条件的头部规则永远不会得到执行。

I'm using a rewrite rule to set an environment variable and then attempting to set cache control headers based on that variable. However, it seems like the variable is being set too late in the process or something; the conditional header rules are never getting executed.

RewriteRule ^cache/.*?/(.*) /$1 [env=asset:true,L]
Header set Cache-control "max-age=30"
Header set Cache-Control "max-age=31536000" env=asset
Header unset ETag env=asset

有没有更好的方法来做到这一点?我试过一对夫妇的目录和定位块的组合,但没有成功。

Is there a better way to do this? I've tried a couple of combinations of Directory and Location blocks with no success.

推荐答案

使用的phpinfo()我确定的环境变量最终没有被设置在所有的重写的请求,所以问题不在于要求的顺序,那就是它似乎折腾变量out。使用查询字符串,而不是URL和的的重写似乎是我能得到这个工作的唯一途径。我不同意,好像有 的应该是一个更好的办法。

Using phpinfo() I determined the environment variable ends up not being set at all on the rewritten request, so the problem isn't the order of the request, it's that it seems to toss the variable out. Using the query string instead of the URL and not rewriting seemed to be the only way I could get this working. I do agree, it seems like there should be a better way.

RewriteCond %{QUERY_STRING} longcache=true(&|$)
RewriteRule .* - [ENV=LONGCACHE:true,L]

Header set Cache-Control "max-age=30" env=!LONGCACHE
Header set Cache-Control "max-age=31536000" env=LONGCACHE

更多不同的答案BY获得的睁眼:

资产环境变量被重命名为 REDIRECT_asset 重定向后,让你的条件标题指令必须是:

Your asset environment variable gets renamed to REDIRECT_asset after the redirect, so your conditional Header directive needs to be:

Header set Cache-Control "max-age=31536000" env=REDIRECT_asset

这篇关于有条件设置缓存头在Apache中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆