0

I have the following directive in my 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>

I copied that regex from someplace in Web months ago. It should add those headers to any HTTP Response that does NOT have those extensions.

But it's not working, it's adding them to any Response.

I also need to create another directive to add Header set Cache-Control "max-age=3600, public" to Responses of files that DOES have them.

Could anybody help me make proper fileMatch regexes?

Hikari
  • 107
  • 3
  • 12

3 Answers3

3

I think that you are looking for this:

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

(?! ...) is special syntax in Perl regular expressions and in PCRE, which is the regex library that Apache uses. It is negative lookahead assertion.

Manolo
  • 552
  • 2
  • 8
  • 23
2

You've got a few capitalization errors in your config:

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

Also, if you've got VirtualHosts, you need to make sure that you've got AllowOverride correctly configured

Jenny D
  • 27,780
  • 21
  • 75
  • 114
  • Thanks for the tip. This htaccess should be transparent for VHs, they are configured elsewhere. In my dev env I have none, in HMP there is 1, in production there are many. There's the need of the application config work anywhere it's deployed. – Hikari Oct 29 '13 at 16:18
  • This is the FilesMatch for static files right? How should it be for excluding these extensions? – Hikari Oct 29 '13 at 16:21
  • Instead of using a separate regexp to exclude the static extensions, start by setting the headers for all files outside of the FilesMatch directive. Then, the headers files matching the regexp for static files will be changed with the FilesMatch. – Jenny D Oct 30 '13 at 08:12
  • I get it, but I'd like to leave default config and set mine to specific content types. – Hikari Nov 04 '13 at 13:04
  • I don't quite understand your meaning. You've got one regexp that matches your static content. As I understood you, you also want one that matches everything else. There's no point in doing that since "everything else" will have the default config. – Jenny D Nov 04 '13 at 13:23
  • ah very sorry! lol now I got your logic – Hikari Nov 05 '13 at 20:02
1

try to use

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

instruction.

Also check that you have properly configured AllowOverride for this virtualhost

cj ayho
  • 86
  • 2