Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

We want to be able to set Cache-Control headers based on User-Agent in Apache

For example, if a User-Agent contains substring foo we want to set Cache-Control to 10 minutes. But if not set it to 1 day.

Searching around, I've found BrowserMatch, but that seems to only set environment variables:

BrowserMatch foo short-live  # Sets environment variable short-live

But I would like to conditionally apply a directive like Header set ... or ExpiresDefault ...

Is there a way to conditionally apply declarations? Something like:

<FilesMatch "\.(jpg|jpeg|gif|png|js|css)$">
  Header set Cache-control "max-age=86400"
  <IfBrowser "foo">
    Header set Cache-control "max-age=600"

Note, IfBrowser is fictional. Is there any real directive that could be used like this? Thanks!

PS This is reposted from since that has received little activity.

share|improve this question
up vote 1 down vote accepted

UPDATED - yes the previous code was no good. However, this might set you in the right direction:

Header set Cache-control "max-age=86400"

SetEnvIfNoCase User-Agent libwww short-cache
Header set Cache-control "max-age=600" env=short-cache

I've actually tested this, it appears to work.

share|improve this answer
No, this will not work, BrowserMatch does not work that way, I'm looking for another selector. Updating question. – sligocki Apr 19 '11 at 19:57
Just curious if the above worked for you or not. – muffinista Apr 20 '11 at 18:13
Great, that's exactly what I was looking for. Thanks! – sligocki Apr 21 '11 at 15:06

Your Answer


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.