Matching Cache-Control headers in .htaccess

I’m trying to take advantage of the Cache-Control headers directive to improve my site performance.

The problem is that I use the minify code to combine and compress my CSS and JS and then apply a timestamp for versioning purposes. This means that the filename doesn’t end in .js or .css - e.g.:

http://www.example.com/min/g=base_css&201005170900
http://www.example.com/min/g=base_js&201005170900

I need help to use the <FilesMatch> regex to properly identify those files - here’s what I currently have -

# BEGIN Cache-Control Headers
<IfModule mod_headers.c>
  <FilesMatch "\\\\.(ico|jpe?g|png|gif|swf)$">
    Header set Cache-Control "max-age=2592000, public"
  </FilesMatch>
  <FilesMatch "_js\\&">
    Header set Cache-Control "max-age=2592000, public"
  </FilesMatch>
  <FilesMatch "_css\\&">
    Header set Cache-Control "max-age=2592000, public"
  </FilesMatch>
  <FilesMatch "\\\\.(x?html?|php)$">
    Header set Cache-Control "max-age=600, private, must-revalidate"
  </FilesMatch>
</IfModule>
# END Cache-Control Headers

unfortunately i’m obviously doing something wrong as this results in:

Cache-Control: max-age=600, private, must-revalidate

Any suggestions?

Why not use Directory or DirectoryMatch to match the “min” directory?
As long as the “min” directory only server js and css files this would work.

FilesMatch “_js” doesn’t work, because “base_js” is part of the query string, not of the file being served. The actual file being served is /min/index.php.
(same goes for “_css” and “base_css” of course).

Btw, minify has its own caching mechanism, you can read about it here.

Thanks for the replies :slight_smile:

ScallioXTX - I think you hit the nail on the head with me looking at the query string rather than the file (duh!).

I modified the config file that set the headers at 1800 to 2592000, removed the dodgy filesmatch directives and also the one for “\\.(x?html?|php)$” (which set the max age to 600 seconds.

Success… :slight_smile: many thanks!

I guess first I’d run your regex attempts through some filter to see what files come up as matches with what you have. Are these minified files the only ones with base_js, base_css etc in them? If so, you might be able to leave off the &. (Does Apache understand lookaheads?)

I mean, if this were mine, that’s the first thing I’d do: check what’s getting matched. Then I’d see what happens if I actually put the full filename in (instead of a regex). If that works, then it really helps narrow down where the problem is. If you put in the exact file name and it doesn’t work, you know the problem’s elsewhere.