Apache 按内容类型设置标题

Apache 按内容类型设置标题,apache,header,apache2,content-type,Apache,Header,Apache2,Content Type,我以前用过这个 <IfModule mod_expires.c> ExpiresActive On ExpiresByType image/gif "now plus 2 weeks" // Lots omitted here </IfModule> 过期于 ExpiresByType image/gif“现在加上2周” //此处省略的地段 而这个, <IfModule mod_headers.c> <filesMatch "\\.

我以前用过这个

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/gif "now plus 2 weeks"
  // Lots omitted here
</IfModule>

过期于
ExpiresByType image/gif“现在加上2周”
//此处省略的地段
而这个,

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

标题集缓存控制“最大年龄=1209600”
//此处省略的地段
我可以按内容类型设置expires,也可以按文件扩展名设置任何标题

但这两种方法似乎都不允许您按内容类型设置任何标题

我想根据响应的内容类型设置缓存控制头-注意,这与文件扩展名不同。我有“友好的URL”,因此
filesMatch
没有要捕获的文件扩展名,因此没有文件扩展名,但内容类型是
text/html


如何设置特定内容类型的缓存控制标头?

在2.4中,可以将
expr=
附加到标头指令,而不是
env=
。例如:

Header set Cache-Control "max-age=3600" "expr=%{CONTENT_TYPE} == 'text/html'"
在默认(非早期)模式下,
mod_headers
作为输出过滤器运行–因此内容类型已经设置好并可由表达式解析器使用


我想您需要先附加或设置缓存控制头。请尝试下面的代码段,不要忘记“no transform”参数


过期于
ExpiresByType image/gif“现在加上2周”
//此处省略的地段
//这就是魔法
标头附加缓存控制“公共,无转换”

如果要创建缓存内容类型,可以通过以下方式输入:

<IfModule mod_expires.c>

ExpiresActive on

ExpiresByType text/html "access plus 15 days"

</IfModule>

过期于
ExpiresByType text/html“访问加15天”

谢谢,但我使用的是2.2版。您的响应中是否存在缓存控制标头?“如何为特定内容类型设置缓存控制标头?”-这是
ExpiresByType指令的功能(具体来说,它设置
缓存控制
头的
max age
指令,以及
Expires
头以实现向后兼容性)。但是,如果您想设置“任意头”或
缓存控制
标题中的特定指令,则需要另一种方法。这将设置
ExpiresByType
标题。这不是我需要的。我需要能够按内容类型设置
缓存控制。@JakeN“这将设置ExpiresByType标题。”-我猜你的意思是
Expires
头,
ExpiresByType
是mod_Expires指令。但即使在Apache 2.0上,这也应该设置
缓存控制
头(通过mime类型/
内容类型
).AFAIK您必须返回到Apache 1.3,以便只设置
Expires
头。这也没有任何帮助。
header append Cache Control“public,no transform”
不考虑内容类型?请注意,在Apache 2.2.12之前,无法使用
header
指令设置
缓存控制
头。
<IfModule mod_expires.c>

ExpiresActive on

ExpiresByType text/html "access plus 15 days"

</IfModule>