According to the apache FilesMatch docs:

The FilesMatch directive provides for access control by filename

Basically, I only want to set an expires header for resources that have a 10 digit "cache buster" id appended to the name. So, here is my attempt at such a thing in my httpd.conf

<FilesMatch "(jpg|jpeg|png|gif|js|css)\?\d{10}$">
    ExpiresActive On
    ExpiresDefault "now plus 5 minutes"

And here is an example of a resource I want to match:


Now obviously my FilesMatch regexp is not matching so I am guessing 1 of 2 things is happening. Either my regexp is wonky or the '?1231231231' cache busting part of the file is not part of what apache considers part of the filename. Can anybody confirm and/or give me a way to cache only those resources that will not persist beyond the next deploy?

migrated from stackoverflow.com Jun 30 '11 at 17:39

This question came from our site for professional and enthusiast programmers.


Ran a simple test and I think I have the answer.

Resource name: http://localhost:3000/stylesheets/baseline.css?1309365344

First Test -- Failed -- This did not cache the baseline.css file

<FilesMatch "baseline.css\?1309365344">
    ExpiresActive On
    ExpiresDefault "now plus 1 week"

Second Test -- Success -- This did cache the baseline.css file

<FilesMatch "baseline.css">
    ExpiresActive On
    ExpiresDefault "now plus 1 week"

Conclusion: When apache says "FilesMatch directive provides for access control by filename", they do not include anything after the extension as part of the filename.

However, I still need to find a way to only apply the ExpiresDefault to resources that has a timestamp on the resource path; I'll put that in another question.

  • This is correct as you have confirmed. FilesMatch ignores the query string. – Elijah Lynn Mar 23 '16 at 22:53

I've just been wrestling with an htaccess problem with a ? in the URL. The ? is not technically part of the filename, but a query string. I wouldn't be surprised if it doesn't get passed to FilesMatch.

The thing that I used was

RewriteCond %{QUERY_STRING} ^something$

Obviously you're not doing rewriting, but you could try and trap the %{QUERY_STRING}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy