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?

3 Answers 3

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

6
  • 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, 2013 at 16:18
  • This is the FilesMatch for static files right? How should it be for excluding these extensions?
    – Hikari
    Oct 29, 2013 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, 2013 at 8:12
  • I get it, but I'd like to leave default config and set mine to specific content types.
    – Hikari
    Nov 4, 2013 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, 2013 at 13:23
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.

2
  • Thanks, could you explain what the ?! means?
    – Hikari
    Nov 5, 2013 at 20:02
  • Answer updated.
    – Manolo
    Nov 6, 2013 at 10:05
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

2
  • While <Files ~ does the same thing as FilesMatch, apache.org recommends using FilesMatch instead.
    – Jenny D
    Oct 29, 2013 at 10:25
  • Yes I'd like to use FilesMatch, but thanks for the tip.
    – Hikari
    Oct 29, 2013 at 16:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .