3

I am trying to configure apache to send expires and cache-control headers on response but only if I receive some specific query string on the request.

For this I've tried configuring the server as follows:

RewriteCond %{QUERY_STRING} ^.*{whateverIWantInMyQueryString}.*$ 
RewriteRule ^(.*)$ - [env=CACHE_HEADERS:1] 

<FilesMatch "\.(js|css|png|gif|GIF)$">
   <IfDefine CACHE_HEADERS>
      ExpiresActive On
      ExpiresDefault "access plus 12 hours"
      Header append Cache-Control "public"
   </IfDefine>
</FilesMatch>

But this does not seem to work.

If I change the configuration as follows:

....
<IfDefine !CACHE_HEADERS>
....

It does work, so it seems IfDefine is not able to check that CACHE_HEADERS environment variable is being set.

I've also tried configuring things with Header directive and conditional based on the same environment variable using something like this:

RewriteCond %{QUERY_STRING} ^.*{whateverIWantInMyQueryString}.*$ 
RewriteRule ^(.*)$ - [env=CACHE_HEADERS:1] 
<FilesMatch "\.(js|css|png|gif|GIF)$">
   Header always set Cache-Control "max-age=3600, public" env=CACHE_HEADERS
   Header always set Expires "Thu, 01 Jan 2015 00:00:00 GMT" env=CACHE_HEADERS
</FilesMatch>
.....

But this does not work either. So I am assuming this might be because of the environment variable that is being set by mod_rewrite, that for some reason is not being detected by neither IfDefine directive nor Header directive.

Does anyone know what may be causing this or why is this not working at all?

fsz
  • 31
  • 3

1 Answers1

0

The reason why IfDefined is not working you can find here: Apache IfDefine conditionals in .htaccess

Basically it's that IfDefined only evaluates variables defined at command line not if you define them like you did or with SetEnv or SetEnvIf.

However as far as I know env= should work.

rugk
  • 4,755
  • 2
  • 28
  • 55