So, I've managed to solve my issues, by taking into account the assumptions I've outlined in my question, which appear to be correct. But not without a struggle.
On trying to implement it for only PHP-generated content, I ran into some other issues, which I'll describe here:
I initially had the following rewrite rules in my .htaccess
:
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
With RewriteRule
flags, you should be able to set environment variables. So, I figured I should be able to set the E=no-gzip:1
flag on merely the last RewriteRule
, by doing:
RewriteRule ^.*$ index.php [NC,E=no-gzip:1,L]
And even though the earlier mentioned
SetEnv no-gzip 1
worked fine, the RewriteRule
flag just wouldn't catch on. Then I looked into all sorts of related questions on Stack Overflow, such as this one this one. But they didn't offer satisfactory solutions.
I then momentarily resorted to setting the environment variable from within PHP with apache_setenv( 'no-gzip', 1 );
, which did the trick. But that just didn't feel right and also made me wonder why the damn RewriteRule
flag wouldn't work just as well.
I then decided to do a var_dump( $_SERVER )
, to see whether the RewriteRule
flag was actually set, only to be confronted with an old nemesis I had forgotten about:
array(39) {
["REDIRECT_no-gzip"]=>
string(1) "1"
["REDIRECT_APP_ENV"]=>
string(11) "development"
/* etc. */
}
The damn REDIRECT_
that gets prepended to environment variables when rewriting.
I was under the impression that the L
flag on my last RewriteRule
should mean stop rewriting, but apparently I had misinterpreted this rule and it's conditions:
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^.*$ - [NC,L]
Since RewriteRule ^.*$ index.php [NC,E=no-gzip:1,L]
rewrites to index.php
, which is a regular file (-s
), the aforementioned conditions and rule still kick in.
Now that I've changed that rule to
RewriteRule !^index.php - [NC,L]
everything is finally working as desired!
Best Answer
If
may be evaluated too early in Apache's processing to use attributes of the response. The documentation isn't clear and I can't find that elsewhere right now.But the
Header
directive is processed late, so it can use the response. You just need to add theexpr
condition to it: