Avoid gzip on images with query string


I know it’s not recommended to gzip images (not sure exactly why though) so I used this code in the httpd config file to gzip everything besides images


Insert filter

SetOutputFilter DEFLATE

Netscape 4.x has some problems…

BrowserMatch ^Mozilla/4 gzip-only-text/html

Netscape 4.06-4.08 have some more problems

BrowserMatch ^Mozilla/4.0[678] no-gzip

MSIE masquerades as Netscape, but it is fine

BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48

the above regex won’t work. You can use the following

workaround to get the desired effect:

BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

Don’t compress images

SetEnvIfNoCase Request_URI
.(?:gif|jpe?g|png)$ no-gzip dont-vary

Make sure proxies don’t deliver the wrong content

Header append Vary User-Agent env=!dont-vary


Everything seems to be working fine, however; I just noticed that images with query strings (like /sample.php?id=123456.jpg) are in fact being gziped (See Results)

is there any way I can filter these out?


You shouldn’t gzip images because they will actually end up larger - they are already compressed by algorithms at least as good as gzip.

I’m a little curious why you have gzip enabled on everything by default and then choose to disable it under certain circumstances. I’m used to seeing it enabled by mime type:

<IfModule mod_deflate.c>
	AddOutputFilterByType DEFLATE text/plain	
	AddOutputFilterByType DEFLATE text/html
	AddOutputFilterByType DEFLATE application/xhtml+xml
	AddOutputFilterByType DEFLATE application/xml
	AddOutputFilterByType DEFLATE text/xml	
	AddOutputFilterByType DEFLATE text/css
	AddOutputFilterByType DEFLATE text/javascript
	AddOutputFilterByType DEFLATE application/javascript
	AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE application/json

Thanks Pat,

I took the code from Apache.org thinking it’s the best source…

Thanks for the code, can this one be used as is in the httpd config file, or only in .htaccess?


It should work fin in the httpd config file as well - http://httpd.apache.org/docs/2.2/mod/core.html#addoutputfilterbytype

using the code you wrote above didn’t work first in httpd config, I changed its location to go after LoadModule deflate_module modules/mod_deflate.so (below all LoadModules) and it works fine now!

if I use the code you provided above; should a file like this/sample.php?id=file-name.jpg” not be gzipped?

I tried it but it still gzips everything:huh:

it’s obviously not being treated like a regular jpg file, because if it was it would have not been gzipped even with my original code
but live headers shows it as a regular image/jpeg

any idea how to fix this?

Any chance your php code is doing the gzip? Just stretching here because the sample I use doesn’t gzip images when I use it but something else is going on since your images are being served from php.

we do not gzip with php, the images are in a folder like this /images/12345.jpg then being modified on the fly with php (adding watermark/resizing/etc.) and called with the php ?id=12345.jpg which serves the modified version

Are you sending the correct mime type in your php script?

header("Content-type: image/jpeg");

Yes, does not seem to help…