Applying Gzip Compression

Gzip Compression & Improving Site Performance

When someone visits your website, he/she sends a request to your server for getting the target file. The size of the file affects the time to download. Before sending your HTML source and style files (.css) to the browser, minimizing their sizes with gzip compression improves page load time seriously. Gzip finds similar lines in a text file and modifies them temporarily. Thus the total size of the file decreases. Especially in HTML and CSS files there can be many similar and repeating lines, so Gzip compression is very useful to shrink their sizes. For this kind of files, you can see a compression rate up to %70.

PS: Gzip can make your site faster but consumes your CPU. Before activating it you should be aware of your CPU usage.

Content:

  • What are the requirements?
  • Step 1 – Activating Gzip Compression
      • Option 1 : by editing .htaccess file
      • Option 2 : CMS plugins
      • Option 3 : with help of Cpanel
      • Option 4 : Windows hosting
  • Step 2 – Testing gzip compression
  • Conclusion

What are the requirements?

Before starting this tutorial you are going to need:

  • .htaccess permission
  • cPanel interface (optional)

Step 1 – Activating gzip compression

You can activate gzip by using mod_gzip and mod_deflate. Mod_deflate compression is advised regarding better compression algorithm and latest apache version compatibility

Option 1: By editing .htaccess file

You can add the code below to your .htaccess file to activate gzip compression with mod_deflate.

<IfModule mod_deflate.c>
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 Older 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
</IfModule>

If your server doesn’t support mod_deflate, you can try the mod_gzip method.

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

When you save the file gzip compression will be enabled.

Option 2: CMS plugins

For the WordPress framework, plugins like “Gzip Ninha Spee Compression” can handle gzip compression. You can find more plugins in WordPress.

For Joomla, under administrator panel “System > General > Server” section can be used to enable gzip compression.

Option 3: With help of cPanel

If you have a cPanel license you can activate gzip compression for all domains under your hosting account.

Option 4: Windows hosting

If you have a Windows Server or VPS, you can enable HTTP compression for static and dynamic content via command line.

appcmd set config /section:urlCompression /doDynamicCompression:True
appcmd set config /section:urlCompression /doStaticCompression:True

Step 2 – Testing gzip compression

Gzip compression can be tested on online tools like WebPageTest and GTMetrix.

Score Result Before Gzip Compression:

Performance result before use gzip

Score After Gzip Compression:

Results after applying Gzip

Conclusion

In this quick tutorial, we tried to improve a website performance regarding speed and bandwidth with gzip compression. The methods like .htaccess, mod_gzip, mod_deflate and different platform Windows and CMS frameworks like WordPress, cPanel were explained.

Leave A Comment?