Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

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>
share|improve this answer
    
Thanks Mark for the response. Your solution seems to be working. –  user15379 Sep 14 '09 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... –  Paul Sheldrake Sep 16 '09 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. –  markdrayton Sep 16 '09 at 8:03
    
+1 for Header set X-Ping "Pong" –  Eddie B Feb 14 '13 at 10:01
add comment

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>
share|improve this answer
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). –  markdrayton Sep 10 '09 at 11:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.