74258

How do I disable GZip with SetEnvIfNoCase in Apache .htaccess?

Question:

I want to disable GZip for certain pages. I have this in my .htaccess, but it still turns GZip on (Content-Encoding: gzip) when visiting dashboard/index.

<ifmodule mod_deflate.c> AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4\.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html SetEnvIfNoCase Request_URI /dashboard/index no-gzip dont-vary

I tried to add Header set MyHeader %{REQUEST_URI} to see what the Request_URI was, but it gave an Internal Server Error.

I also tried the regex dashboard/index, dashboard/index.*, "/dashboard/index", etc., and tried SetEnvIfNoCase REQUEST_URI ..., but GZip was still on.

If I comment #AddOutputFilterByType, then GZip is turned off.

I'm using Apache 2.4.16, Yii 2.0.7, PHP. I'm using FPM in production, so apache_setenv() is not available.

Answer1:

You're probably using rewrites to get rid of index.php in the URL. Due to the stage at which SetEnvIf runs during the request, index.php will be part of the Request_URI var used (which is distinct from %{REQUEST_URI}).

It's nowadays quite common to not use PATH_INFO for rewrites, but just plainly rewrite to index.php, where code just reads the original REQUEST_URI info. In that case, Request_URI in SetEnvIf will be just "index.php", so you'd need to set a flag env var in a special dummy rewrite for that URL, and reference it later with a REDIRECT_ prefix (as there is an internal redirect stage on rewrites where mod_rewrite prefixes all existing env vars with REDIRECT_):

RewriteRule ^dashboard/index - [E=no-gzip:1] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule . index.php [L] SetEnvIf REDIRECT_no-gzip 1 no-gzip

There is a slightly less verbose way if you rewrite to PATH_INFO (so "/foobar" turns to "/index.php/foobar" using e.g. a RewriteRule (.*) index.php/$1 rule):

RewriteCond %{REQUEST_FILENAME} !-f RewriteRule (.*) index.php/$1 [L] SetEnvIfNoCase REQUEST_URI ^/index.php/dashboard/index.*$ no-gzip dont-vary

But that seems more brittle since it'll break if you change the RewriteRule mechanics.

Recommend

  • C++ two dimensional array in shared memory (based on command line argument) [closed]
  • php 5.5.9 gives error when subclass is declared before parent
  • How to use RelativeSource Binding to create DataGrid binding to Model and ViewModel?
  • Authenticating WCF DataServices
  • SilverStripe. How to redirect from www.domainName to domainName?
  • converting string representation of unknown date-format to Date in java
  • Scrapy store returned items in variables to use in main script
  • Is there any way to whitelist .appspot.com so I can set a cookie on that domain in Firefox 6?
  • bookmark feature opening as a sidebar
  • Javascript : White space string boolean conversion
  • Append data to file with firefox extension
  • Nginx custom PHP error_pages with different header status codes and body contents
  • How can I .htaccess 301 redirect all pages/files (except for one) to new domain?
  • .each() with a filter
  • How to get element by point in jQuery/Javascript
  • jQueryUI tabs and Firefox: getBBox broken?
  • keyDown event is not fired for Capslock in Mac
  • Unable to change camera position when using VRControls
  • How to embed flash in Mono?
  • How to convert list into string with quotes in python
  • use rvest and css selector to extract table from scraped search results
  • Redirect to trailng slash (htaccess)
  • JSON data through JS/AJAX into PHP
  • how to display   in Mozilla using XSL.
  • calculating number of bytes of each row in an image
  • Varnish/Apache Random 503 Errors
  • How to make jdk.nashorn.api.scripting.JSObject visible in plugin [duplicate]
  • saving file generated by TCPDF
  • How to view images from protected folder with php?
  • Spring Cloud Microservice Architecture Confusion
  • Textfile Structure (tables)
  • vba code to select only visible cells in specific column except heading
  • How to set the response of a form post action to a iframe source?
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Change div Background jquery
  • Android Google Maps API OnLocationChanged only called once
  • Qt: Run a script BEFORE make
  • reshape alternating columns in less time and using less memory
  • UserPrincipal.Current returns apppool on IIS