我以前用过这个;
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/gif "now plus 2 weeks"
// Lots omitted here
</IfModule>还有这个;
<IfModule mod_headers.c>
<filesMatch "\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|JPG)$">
Header set Cache-Control "max-age=1209600"
</filesMatch>
// Lots omitted here
</IfModule>我可以按内容类型设置expires,也可以按文件扩展名设置任何头文件。
但是这两个似乎都不能让你根据内容类型设置任何你想要的头部。
我想根据响应的内容类型设置cache-control头-注意,这与文件扩展名不同。我有“友好的URL”,所以没有文件扩展名被filesMatch捕获,所以没有文件扩展名,但内容类型是text/html。
如何为特定的内容类型设置缓存控制标头?
发布于 2014-02-10 08:00:51
在2.4中,您可以将expr=而不是env=附加到Header指令。例如:
Header set Cache-Control "max-age=3600" "expr=%{CONTENT_TYPE} == 'text/html'"在默认(非早期)模式中,mod_headers作为输出过滤器运行-因此内容类型已经设置,并且可由表达式解析器使用。
http://httpd.apache.org/docs/2.4/expr.html
发布于 2014-02-25 22:38:11
我猜您需要首先附加或设置Cache-Control头。请尝试下面的代码片段,不要忘记"no-transform“参数。
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/gif "now plus 2 weeks"
// Lots omitted here
//This is the magic
<IfModule mod_headers.c>
Header append Cache-Control "public, no-transform"
</IfModule>
</IfModule>发布于 2014-02-25 22:40:00
如果你想把缓存设置为内容类型,你可以这样输入:
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType text/html "access plus 15 days"
</IfModule>https://stackoverflow.com/questions/21663326
复制相似问题