Conditionally setting cache headers in apache

感情迁移 提交于 2019-12-20 12:22:32

问题


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.


回答1:


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

MORE DIFFERENT ANSWER OBTAINED BY OPENING EYES:

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


来源:https://stackoverflow.com/questions/7812407/conditionally-setting-cache-headers-in-apache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!