Search code examples
apachecachinghttp-headerscache-control

Apache - Add Cache-Control header if not already added


In Apache, how can I set a Response Header only if it is not already set by the CGI application?

I need a way to automatically add the Cache-Control header to static content on my website, but I want the CGI application to be able to specify its own Cache-Control header too.

SetEnvIf will not work for this purpose because it only matches Request Headers.

Is there some way to conditionally / optionally set a header if it is not already set?


Solution

  • 1) Configure Apache to append the value of empty string ("") to the Cache-Control header to ensure the header is always included in the response.

    2) Configure Apache to set the Cache-Control header only if it's still set to empty string.

    <FilesMatch "\.(css|ico|flv|gif|jpeg|jpg|js|pdf|png|swf)$">
      Header append Cache-Control ""
      Header edit Cache-Control "^[, ]*$" "max-age=1800, public"
    </FilesMatch>