Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

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

share|improve this answer
    
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 8:12
    
I get it, but I'd like to leave default config and set mine to specific content types. –  Hikari Nov 4 '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 4 '13 at 13:23
show 1 more comment

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

share|improve this answer
    
While <Files ~ does the same thing as FilesMatch, apache.org recommends using FilesMatch instead. –  Jenny D Oct 29 '13 at 10:25
    
Yes I'd like to use FilesMatch, but thanks for the tip. –  Hikari Oct 29 '13 at 16:15
add comment

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.

share|improve this answer
    
Thanks, could you explain what the ?! means? –  Hikari Nov 5 '13 at 20:02
    
Answer updated. –  Manolo Salsas Nov 6 '13 at 10:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.