Enable GZip or DEFLATE compression of text-based responses in order to reduce file size and bandwidth. Can have significant impact on the perceived speed of website page loads.

Compress using mod_deflate

Using DEFLATE for compression is typically preferred over GZip given its broader browser support historically. Ultimately, the amount of compression you’ll see from either module will be essentially the same, so it more comes down to what may already be enabled on your server (or which you choose to enable).

To enable compression using the mod_deflate module, use the following code (the mod_deflate module must be enabled):

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/opentype 
</IfModule>

Compress using mod_gzip

To enable compression using the mod_gzip module, use the following code (the mod_gzip module must be enabled):

<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 mime ^application/x-javascript.*
mod_gzip_item_include mime ^text/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_include handler ^cgi-script$
</ifModule>

If using a server-side language whose extension is not listed in the file extension list above, simply add its extension to the pipe-delimited list.

In More Detail

Web servers leverage gzip to compress data returned to clients. When a gzip-enabled browser sends a request, it adds ‘gzip’ to its Accept-Encoding request header.

When the web server receives the request it processes the response as normal and then checks the Accept-Encoding header to determine how to encode the response. If the requesting browser supports gzip and the server also supports gzip, it compresses each compressible resource using the gzip algorithm and returns those assets with an added Content-Encoding header, specifying that the resource has been encoded using gzip. The browser then decompresses the compressed content into its original uncompressed format before rendering it in the browser.

Note that there is some cost associated with this process. Compression is a CPU-intensive process, and the more you compress a response, the longer it will take. Given this size vs speed trade-off, gzip offers compression levels from 1 to 9, where 1 provides the fastest speed but with a lower compression ratio, and 9 offers the highest compression ratio but at a slower speed. By default, the setting is typically 6 to slightly prefer smaller file sizes over speed.