WordPress – How to set an environment variable based on pre-rewritten URI or post-rewritten query parameter

.htaccessapache-2.2gziprewriteWordpress

So there's a file in WordPress, wp-content/blogs.php, that serves files uploaded through WordPress. There's a RewriteRule in the WP .htaccess that rewrites URLs of the format www.example.com/files/11/01/filename.mp3 to URLs of the format www.example.com/.../wp-content/blogs.php?file=filename.mp3.

RewriteRule ^(.*/)?files/(.*) wp-content/blogs.php?file=$2 [L]

So here's the problem: we have gzip turned on for PHP files. I need to turn it off based on the query parameter file, on blogs.php. (Not just because you're not supposed to gzip binaries, but because gzipping .mp3 creates severe problems for the Quicktime plugin in Firefox on both PC and Mac.)

But the following SetEnvIf isn't applied to any files served through blogs.php – the Request_URI always ends in .php.

SetEnvIfNoCase Request_URI \.mp3$ no-gzip dont-vary

The following fixes the problem by turning off gzip on all files served through blogs.php:

<Location /michael/blah/wp-content/blogs.php>        
     SetEnv no-gzip
</Location>

But it's not a good solution, because I want non-binary files served through blogs.php to be gzipped. So what I really want is a way to get the original URI, not the rewritten URI, because the original URI does end in .mp3. Or, I need to access the query parameter at the end of the rewritten URI (ie. file=filename.mp3).

For instance, something like this would work, if it existed, which it doesn't:

SetEnvIfNoCase Original_Request_URI \.mp3$ no-gzip dont-vary

It looks like there's a variable THE_REQUEST that would work in a RewriteRule, but it doesn't seem available to SetEnvIf.

Any ideas?

Best Answer

You can manipulate the environment from mod_rewrite, including turning off gzip:

RewriteCond %{QUERY_STRING} file=(.*)\.mp3$
RewriteRule ^(./)?files/(.) wp-content/blogs.php?file=$2 [L,E=no-gzip:1]

The first RewriteCond may not be necessary, but the last rule (the E flag) should do the trick in your setup regardless.

HTH.