Speed up your website and use gzip compression using the .htaccess file in the root of your website’s directory if you are using Apache or NGIX (Linux).

You can actually use two different Apache mods to enable HTTP gzip compression: mod_gzip and mod_deflate. Mod_gzip enables gzip compression and mod_deflate makes it possible to compress the output from your server before it is being sent to your visitor it is the same thing.

Enabling gzip compression or deflate can reduce the size of the transferred response by up to 90%, which can significantly reduce the amount of time to download the resource and reduce data usage.

What deflate does is, it will compress the requested page on your server and send it to the visitor’s web browser which will decompress it, so the transfer will be much faster. Use one of the codes hereunder and add it to the .htaccess file:

#Begin gzip and deflate
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE image/x-icon
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
# For Olders Browsers Which Can't Handle Compression
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
#End gzip and deflate

 

Second option

You can also use this code if the first one doesn’t work, sometimes it happens it depends on the server configuration, we use the code hereunder on most of our websites. Copy and add it to your .htaccess file but never use both:

#Begin gzip and deflate
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$
#End gzip and deflate

 

Note: you can add or remove certain file types from the above examples if you don’t or do want to use it.

We use this one

We use this code most out of the cases we added some more extensions to fit our needs (it includes xml and work on most servers) and is clean and short:

#Begin gzip and deflate
<filesMatch "\.(html?|txt|css|js|php|pl|xml)$">
SetOutputFilter DEFLATE
#End gzip and deflate

If you enable gzip compression the next step is: Leverage browser caching

Check this article on what to add to your .htaccess file to let the client cache your images, scripts or files so the visitor’s browser doesn’t download the same files over and over again: Enable Leverage browser caching using the .htaccess file for Apache