htaccess filesMatch exclusion

7,151

Solution 1

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

Solution 2

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.

Solution 3

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:
7,151

Related videos on Youtube

Hikari
Author by

Hikari

Updated on September 18, 2022

Comments

  • Hikari
    Hikari almost 2 years

    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
    Hikari over 10 years
    Yes I'd like to use FilesMatch, but thanks for the tip.
  • Hikari
    Hikari over 10 years
    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
    Hikari over 10 years
    This is the FilesMatch for static files right? How should it be for excluding these extensions?
  • Jenny D
    Jenny D over 10 years
    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.
  • Hikari
    Hikari over 10 years
    I get it, but I'd like to leave default config and set mine to specific content types.
  • Jenny D
    Jenny D over 10 years
    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.
  • Hikari
    Hikari over 10 years
    ah very sorry! lol now I got your logic
  • Hikari
    Hikari over 10 years
    Thanks, could you explain what the ?! means?
  • Manolo
    Manolo over 10 years
    Answer updated.