wp knowledge mascot compression anvil

How to Enable GZIP Through the .htaccess File

Last Updated on April 20, 2022 by WP Knowledge Hub

What exactly is GZIP Compression?

Compression by Gzip uses a simple algorithm which organizes repeated strings in one place, and rather than saving them repeatedly, it saves them in a single place.

The Gzip compression algorithm manages strings and their location values while compressing and retrieving data from files. Gzip compression works with stylesheets and web pages because they constantly repeat strings. Gzip will most likely reduce file sizes by up to 90%.

Step 1:

The first step is to log into your cPanel (or Plesk or other custom hosting panels), or access the server files with FTP (FileZilla or Cyberduck, etc.). If you’re using a hosting panel, you’ll want to locate the File Manager and access your root directory. If you’re using an FTP client, you’ll most likely be logged into the root folder by default.

Next you’ll want to locate the  .htaccess file located in the root directory.

Step 2:

Open the .htaccess file directly in the File Manager by clicking EDIT, or download it to your local computer.

Add this code at the end of your .htaccess document:

<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  

  # Remove browser bugs (only needed for ancient 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>

If your website is hosted on a Nginx server, use this one instead:

gzipon;
gzip_comp_level2;
gzip_http_version1.0;
gzip_proxied any;
gzip_min_length1100;
gzip_buffers168k;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_disable"MSIE [1-6].(?!.*SV1)";
gzip_varyon;

Step 3:

Make the change, re-upload, and overwrite the previous one.

Once you’ve overwritten the previous .htaccess file, go to gtmetrix.com and scan your site again. You should see a significant score improvement.