9

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

2 Answers 2

5

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>
5
  • Thanks Mark for the response. Your solution seems to be working.
    – user15379
    Sep 14, 2009 at 12:03
  • Does something need to be enabled for LocationMatch to work? I tried using this solution and it causes an internal error but FileMatch works... Sep 16, 2009 at 6:36
  • No, they're both part of the server core. Open another question with your configuration snippet and the error message and we'll take a look. Sep 16, 2009 at 8:03
  • 1
    +1 for Header set X-Ping "Pong" Feb 14, 2013 at 10:01
  • does this cache response or it will gzip in each subsequent request to same js/css file?
    – amit patel
    Jan 26, 2016 at 15:56
0

The $ at the end of your regular expression will match the end of the URL, so if there is anything after the file extension, it won't match. You could omit the $ but then it would match if there was a ".css" etc. anywhere in the URL. Something like the example below might be safer. This should match either ".css" at the end of URL, or ".css" followed by ?, some parameters, and then the end of the URL.

   <IfModule mod_deflate.c>
        <FilesMatch "\.(css|js|x?html?|php)($|\?.*$)">
           SetOutputFilter DEFLATE
        </FilesMatch>
   </IfModule>
1
  • 1
    I don't think that's correct. FilesMatch matches a filesystem filename, not a URI. LocationMatch matches URIs (but even that only looks at the URI, not the query string). Sep 10, 2009 at 11:53

You must log in to answer this question.