The cart is empty

Gzip compression is a crucial technique for speeding up the loading of web pages. By using gzip compression, we can significantly reduce the size of data transferred between the server and the client, leading to faster page load times and a better user experience. This article provides a detailed guide on how to set up gzip compression using the .htaccess file.

What is Gzip Compression?

Gzip is a file compression algorithm that reduces the size of text files, such as HTML, CSS, and JavaScript. Compressed files are then transmitted more quickly over the network, shortening the load time of web pages.

Prerequisites

Before setting up gzip compression, ensure you have access to the .htaccess file on your web server. This file is usually located in the root directory of your web application.

Basic Gzip Compression Setup

Open the .htaccess file in your text editor and add the following code:

<IfModule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML, and Fonts
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml

  # Handle outdated browsers
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent
</IfModule>

Code Explanation

  • <IfModule mod_deflate.c>: Checks if the mod_deflate module is loaded.
  • AddOutputFilterByType DEFLATE: Specifies the file types to be compressed.
  • BrowserMatch: Resolves compatibility issues with older browsers.
  • Header append Vary User-Agent: Adds a header to ensure correct compression for different browsers.

Advanced Gzip Compression Settings

To further optimize gzip compression, you can add additional settings. For example, you can exclude certain files or directories from compression:

<IfModule mod_deflate.c>
  # Basic gzip compression settings
  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript

  # Exclude specific file types
  SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary

  # Exclude specific directories
  SetEnvIfNoCase Request_URI ^/exclude-directory/ no-gzip dont-vary
</IfModule>

 

Setting up gzip compression in the .htaccess file is an effective way to improve the performance of your web pages. This process involves editing the .htaccess file and adding the necessary directives for file compression. Regular testing and optimization ensure that your pages remain fast and user-friendly.