2

I've found example of how to do this all over the web, but I haven't been able to get this to work.

Want to set a long future cache time on requests that have a query string of something like ?v=123. I've included my configuration below. But it just won't work. What am I doing wrong?

RewriteEngine on
RewriteCond %{QUERY_STRING} ^v
RewriteRule ^.*$ - [ENV=LONGCACHE:1]
Header set Cache-Control "max-age=31536000, public" env=LONGCACHE

I've tried a bunch of different combination on the regex testing the query string, nothing seems to work.

marcusds
  • 207
  • 2
  • 9

2 Answers2

1

Turns out this config needs to be in side the <VirtualHost>, which is a pain since I was trying to load from conf.d. But at least if anyone else runs into this issue, you need to have this in your VirtualHost!

marcusds
  • 207
  • 2
  • 9
1

Turns out this config needs to be inside the <VirtualHost>, which is a pain since I was trying to load from conf.d

Not necessarily.

(Knowing that these directives were in the main server config, outside of the existing vHost, is an important point missing from your question.)

"The problem" is that the mod_rewrite directives in the server config are not inherited by the <VirtualHost> container by default. The Header directive (part of mod_headers) in the server config is processed, but since the mod_rewrite directives have not run and not set the LONGCACHE environment variable, the header is not set.

If you were on Apache 2.4 then you can use SetEnvIfExpr (mod_setenvif) to set the environment variable based on the presence of the query string, and avoid using mod_rewrite.

On Apache 2.2 you would need to enable mod_rewrite inheritance in the <VirtualHost> container:

RewriteEngine On
RewriteOptions Inherit

Note, however, that the directives from the server config are inherited after the directives in the virtualhost context (NB: directives in <Directory> containers do not apply as they run later - in a directory context). This means that directives in the vHost could potentially prevent the server directives from being executed. (On Apache 2.4+ you have more control over how the directives are inherited.)

OR, include these "shared" directives in a separate config file and include the config file in each vhost as required.

MrWhite
  • 12,647
  • 4
  • 29
  • 41