enable gzip compression on apache server

How to enable GZip compression on Apache Server?

How to enable GZip compression on Apache Server

How to enable GZip compression on Apache Server

GZip compression is used to increase speed of web page in your site. Your browser send request to server and get all the images, javascript and css files from server. When your web page have lots of images, javascript and css files than its take time to load. GZip compression gives boost to your website speed and save bandwidth as well.
In a very simple language, GZip compress all the data and send to browser. All the latest browser understands and accept compressed files.
There are two major ways to enable the GZip Compression on the Apache Server web-sites.

1) Using httpd

You need to install mod_deflate module, if it is not installed than you can download it from Apache httpd Modules and add it to httpd modules directory on your server.

Once you add mod_deflate module in your httpd modules directory, you completed half work. Now open your “httpd.conf” file and search for “mod_deflate”. You will find the following line

LoadModule deflate_module modules/mod_deflate.so

If it is commented than uncomment it, so it will LoadModule directive for mod_deflate.

Now we need to create a Deflate Rule Set which tells your apache server that compress which types of file.

Create “httpd-deflate.conf” file and copy following code in that file

#Set GZip for all output
SetOutputFilter DEFLATE
#Not Include the below file types
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|iso|tar|bz2|sit|rar|png|jpg|gif|jpeg|flv|swf|mp3)$ no-gzip 
#Use compression Level
DeflateCompressionLevel 9
#You can Handle browser specific compression using following rules
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0

Now, open “httpd.conf” file and add following code

# Deflate configuration
include <path of file>/httpd-deflate.conf

Now restart your Apache Server and its working GZip Compression. Your website will load 60% faster than before.

2) Using .htaccess file

You can add following code in your htaccess file

<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.*

If above code is not working than you can try another code which is as below

<ifModule mod_gzip.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

Leave a Reply

Your email address will not be published. Required fields are marked *