Compress with Gzip or Deflate my CSS & JS files

12,006

phpinfo doesn't show Apache's configuration unless it's a module - mod_status shows Apache's configuration, including loaded modules.

If you have mod_gzip installed, this should work:

mod_gzip_on Yes
mod_gzip_dechunk Yes

mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/javascript
mod_gzip_item_include mime ^application/xm.*
mod_gzip_item_include mime ^image/svg.*
mod_gzip_item_include mime ^application/(x-)?font-.*

(Note, the first two lines grab CSS and Javascript, but also anything with a "text" mime type)

I prefer mod_gzip to mod_deflate as

  1. GZip has a checksum, and uses the exact same compression as deflate.
  2. The configuration is a million times simpler. I still can't figure out if the Deflate configuration was intentionally difficult or an artifact of history.
  3. GZip is just as well supported in browsers, so no drawbacks other than the "overhead" of the checksum.
Share:
12,006

Related videos on Youtube

muhammad usman
Author by

muhammad usman

Updated on September 18, 2022

Comments

  • muhammad usman
    muhammad usman over 1 year

    i ve a fashion website & using wordpress. I want to Compress or Gzip or Deflate my CSS & JS files. i have tried many codes with .htaccess to compress but not working. Would any body help me please?

    My phpinfo is http://deemasfashion.co.uk/1.php

    below are the codes i have tried not not working. Few of them might be same but there is a difference in the syntax.

    <ifModule mod_gzip.c>
    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
    mod_gzip_item_include handler ^cgi-script$
    mod_gzip_item_include mime ^text/.*
    mod_gzip_item_include mime ^application/x-javascript.*
    mod_gzip_item_exclude mime ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
    </ifModule>
    

    other code I have tried but not working...

    <files *.css>
    SetOutputFilter DEFLATE
    </files>
    <files *.js>
    SetOutputFilter DEFLATE
    </files>
    

    I have also tried this code as well but no success.

    <ifModule mod_gzip.c>
    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
    mod_gzip_item_include handler ^cgi-script$
    mod_gzip_item_include mime ^text/.*
    mod_gzip_item_include mime ^application/x-javascript.*
    mod_gzip_item_exclude mime ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
    </ifModule>
    

    This code is also not working

    <FilesMatch "\.(html?|txt|css|js|php|pl)$">
    SetOutputFilter DEFLATE
    </FilesMatch>
    

    Here is another code not working.

    <ifmodule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x- javascript application/javascript
    </ifmodule>
    

    Here is another code not working.

    <IFModule mod_deflate.c>
    <filesmatch "\.(js|css|html|jpg|png|php)$">
    SetOutputFilter DEFLATE
    </filesmatch>
    </IFModule>
    

    Here is another code not working.

    <IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/x-javascript text/javascript    application/javascript application/json
    <FilesMatch "\.(css|js)$" >
    SetOutputFilter DEFLATE
    </FilesMatch>
    </IfModule>
    

    Here is another code not working.

    #Gzip - compress text, html, javascript, css, xml
    <ifmodule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
    </ifmodule>
    #End Gzip
    

    Here is another code not working.

    <Location />
    SetOutputFilter DEFLATE
    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|gz2|sit|rar)$ no-gzip dont-vary
    </Location> 
    
    • Philip
      Philip over 10 years
      Have you tried asking your hosting provider if they support GZip or Deflate??
    • muhammad usman
      muhammad usman over 10 years
    • muhammad usman
      muhammad usman over 10 years
      @Chris S yes they do its a managed VPS with Cpanel. i ve also asked them
  • muhammad usman
    muhammad usman over 10 years
    thanks @Chris S i have used phpinfo(INFO_MODULES); in the link does this show the mod-status or there is another function.
  • muhammad usman
    muhammad usman over 10 years
    .should i change the PHP run through mod_php or is there any way that I can Gzip the CSS and JS. Currently Gzip / Zlib is enabled in my Cpanel / Whm. My php pages are Gziped but only CSS and JS are not.
  • muhammad usman
    muhammad usman over 10 years
    please test this Fashion
  • muhammad usman
    muhammad usman over 10 years
    Would you please check this now
  • Philip
    Philip over 10 years
    I'm sorry, but it has become apparent that you are an end user and not a system administrator. Please refer to your hosting company for support implementing your website on their servers.
  • muhammad usman
    muhammad usman over 10 years
    some OP or senior citizen referred me from stack overflow to serverfault by saying that this question is relevant to serverfault. :)
  • Philip
    Philip over 10 years
    I'm sorry but Stack Overflow has a habit of telling people incorrect information about other Stack Exchange sites. How do you know the code is working nicely if you don't know how to verify if GZip is working?? Install a HTTP Header plug-in for your browser and see what the Content-Encoding: of the server response says. LiveHTTPHeaders for Firefox or Chrome.
  • Anuj TBE
    Anuj TBE over 8 years
    Thanx... It worked for me.. I also have tried all options given in question. But, finally got this working.