htaccess filesMatch排除

我在我的htaccess中有以下指令

<filesMatch "\.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml|html?)$"> FileETag None <ifModule mod_headers.c> Header unset ETag Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate" Header set Pragma "no-cache" Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT" </ifModule> </filesMatch> 

我在几个月前从Web的某个地方复制了这个正则expression式。 它应该将这些头添加到任何没有这些扩展的HTTP响应。

但它不工作,它将它们添加到任何响应。

我还需要创build另一个指令,将Header set Cache-Control "max-age=3600, public"到具有它们的文件的响应中。

任何人都可以帮我做适当的fileMatch正则expression式吗?

你的configuration中有几个大写错误:

 <filesMatch "\.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml|html?)$"> ^ should be <FilesMatch ... <ifModule mod_headers.c> ^ should be <IfModule... </ifModule> ^ should be </IfModule> </filesMatch> ^ should be </FilesMatch> 

另外,如果你有VirtualHosts,你需要确保你已经正确configuration了AllowOverride

尝试使用

 <Files ~ "\.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml|html?)$"> 

指令。

同时检查您是否为这个虚拟主机正确configuration了AllowOverride

我认为你正在寻找这个:

 <FilesMatch ".*$"> Header unset ETag Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate" Header set Pragma "no-cache" Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT" </FilesMatch> <FilesMatch "(?!\.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml|html?))$"> FileETag None <IfModule mod_headers.c> Header set Cache-Control "max-age=3600 </IfModule> </FilesMatch> 

(?! ...)是Perl正则expression式和PCRE中的特殊语法,PCRE是Apache使用的正则expression式库。 这是消极的前瞻断言。