Why Apache doesn’t gzip css or js files which have parameters

apache-2.2gzipmod-deflate

Some CSS & JS files are not compressed by apache with mod_deflate enabled. This files looks like this "[domain.name]/aggregator.css?…" or "[domain.name]/misc/jquery.js?…" in YSlow. The other CSS & JS without the "?" gets compressed. Kindly explain me how do I make apache compress this files also.

I added the following line on the .htaccess file

  <IfModule mod_deflate.c>
        <FilesMatch "\.(css|js|x?html?|php)$">
           SetOutputFilter DEFLATE
        </FilesMatch>
  </IfModule>

Thanks

Best Answer

Hrm, interesting. I can't reproduce this:

[root@dev ~]# cat /etc/httpd/conf.d/test.conf
<FilesMatch "\.txt$">
  SetOutputFilter DEFLATE
  Header set X-Ping "Pong"
</FilesMatch>

[root@dev ~]# GET -SedH 'Accept-Encoding: gzip, deflate' 'http://localhost/test.txt'
GET http://localhost/test.txt --> 200 OK
Content-Encoding: gzip
Content-Length: 2449
X-Ping: Pong

[root@dev ~]# GET -SedH 'Accept-Encoding: gzip, deflate' 'http://localhost/test.txt?foo'
GET http://localhost/test.txt?foo --> 200 OK
Content-Encoding: gzip
Content-Length: 2449
X-Ping: Pong

(unrelated headers removed)

As mentioned above, FilesMatch matches filesystem paths, not URIs. Are the resources that aren't being compressed files on the filesystem or are they generated on the fly, or proxied, or redirected or something else that breaks direct URI-to-filesystem mapping? You could try using LocationMatch instead, which is probably a better bet anyway if any part of your app isn't static files.

<IfModule mod_deflate.c>
  <LocationMatch "\.(css|js|x?html?|php)$">
     SetOutputFilter DEFLATE
  </LocationMatch>
</IfModule>