How to enable gzip?

In Apache, enabling output compression is fairly straightforward. Add the following to your .htaccess file:

# compress text, html, javascript, css, xml:
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

# Or, compress certain file types by extension:
<files *.html>
SetOutputFilter DEFLATE
</files>

Source : http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/


Have you tried with ob_gzhandler?

<?php ob_start("ob_gzhandler"); ?>
<html>
  <body>
    <p>This should be a compressed page.</p>
  </html>
<body>

As an alternative, with the Apache web server, you can add a DEFLATE output filter to your top-level server configuration, or to a .htaccess file:

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

Tip: Sometimes it is pretty tricky to detect if the web server is sending compressed content or not. This online tool can help with that.

Using the developer tools in my web browser, I tested a PHP file with and without compression to compare the size. In my case, the difference was 1 MB (non-compressed) and 56 KB compressed.


All I had to do to enable the encoding at the Apache level is

zlib.output_compression = 1 // the PHP.ini file

this will make the server do the necessary request header check, compress, send related headers

you can also do that in your PHP files before the ob_start()

ini_set("zlib.output_compression", 1);

And to make Apache compress the static resources (e.g: .js files , .css files) do as Kamlesh did in his answer


In the official wiki of Dreamhost they enable this by modifying an htaccess:

<IfModule mod_gzip.c>
    mod_gzip_on       Yes
    mod_gzip_dechunk  Yes
    mod_gzip_item_include file      \.(html?|txt|css|js|php|pl|jpg|png|gif)$
    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 basically checks to see if mod_czip.c is found and if it is it will compress the files for you so they are faster to send to the browser. This supposedly speeds up download times 35-40%, and then the file size should supposedly go down to 55-65%.

With a quick search on Google you can come up with another thread on Stackoverflow an in a third party site addressing this issue.