How to Enable GZIP Compression for WordPress

How to Enable GZIP Compression for WordPress

A slow website can create a lot of problems. It can drive away potential visitors and customers, hurt your site’s User Experience (UX), and even impact your reputation.

Decreasing the size of your website’s pages is key to improving load speeds. Fortunately, GZIP compression is a great method for achieving fast page loading and file size reduction. 

In this guide, we’ll look at the basics of GZIP compression, including what it is and what it does. We’ll then detail how to enable GZIP compression on a WordPress website. Let’s jump right in!

What is GZIP Compression?

GZIP compression is a type of data compression similar to ZIP and RAR. It can be used to compress all the files on your website, whether they’re text, HTML, CSS, JavaScript, or XML.

GZIP compression occurs before files are served to your visitors’ web browsers. Through this method, GZIP compression reduces the file sizes, which can have a positive effect on website loading times.

What Does GZIP Compression do?

When a visitor lands on your website, their browser requests the site’s files from its server. Your server will compress those files before sending them to the browser. GZIP compression is one method of file compression amongst others.

Compressed files transfer faster than uncompressed files do. This helps to improve website performance and loading speeds – and as a result, Search Engine Optimization (SEO).

How to See If GZIP Compression Is Enabled

GZIP compression is popular, and many WordPress hosting platforms enable it by default. If you are unsure how to check if GZIP compression is working on your site, you can test for it using third-party solutions or in-browser developer tools. 

If you do not have GZIP compression enabled, you will generally see warnings in tools such as GTmetrix. You can also use a GZIP compression tool to see whether your website is using GZIP compression. 

It is also possible to check if GZIP compression is working by using browser developer tools. When enabled, the response header will include “content-encoding: gzip”:

To find the response header in Chrome, you will need to open DevTools and navigate to Network, then reload the page. Click on the name that matches your website, and the response header will open up.

How to Enable GZIP Compression on WordPress

All websites on WP Engine use GZIP compression as a default. All static files including text files, images, CSS, and JavaScript are automatically compressed for optimal website performance. This automatic compression reduces the need for GZIP directives in your .htaccess file. 

However, not all WordPress websites are hosted on WP Engine, and will not benefit from the default enabling of GZIP compression. If your website is hosted on a different platform, you may need to enable GZIP compression manually. There are several ways to do this, which we’ll look at next.

Enable GZIP Compression with a Plugin

The easiest way to enable GZIP compression is to use a caching plugin. Tools such as WP Rocket enable compression by adding code to your .htaccess file for you, while others such as W3 Total Cache will require you to manually add that code.

While this is generally the simplest method of adding GZIP compression, it does involve adding an extra plugin to your site. If you’re trying to keep your plugin library lean, you can use one of the following solutions instead.

Enable GZIP Compression for Nginx

If your website uses Nginx, there is a code snippet you can use to enable GZIP compression. You need to place the code snippet below in your site’s nginx.conf file, in the “http” section. You can also place this code snippet in the server or location configuration block, but the http section is generally recommended:

gzip on;
gzip_types text/plain text/css text/javascript image/svg+xml image/x-icon application/javascript application/x-javascript;
gzip_min_length 1000;
gzip_vary on;
gzip_proxied no-cache no-store private expired auth;
gzip_diable “MSIE [1-6];

Once you’ve saved and closed the file, you will need to restart Nginx. To do so, use the following command:

sudo service nginx restart

After the restart, check to make sure that GZIP compression is active, using the methods previously detailed.

Enable GZIP Compression for Apache (By Editing the .htaccess File)

If your web host uses Apache, you will need to edit the .htaccess file to enable GZIP compression. This file can be found in the root directory of your WordPress installation, via File Transfer Protocol (FTP) or your file manager. Most WordPress GZIP compression methods will require editing the .htaccess file.

There are two Apache mods you can use to enable GZIP, but mod_deflate is the most widely-supported and best-documented option. To enable mod_deflate, add:

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

To enable mod_gzip instead, add:

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

Before using either mod, you need to make a backup of your .htaccess file and ensure that the module mod_filter is on your server, or a 500 error may occur.

Keep Learning with WP Engine

Reducing the size of your website’s pages can reduce loading times and boost overall performance. GZIP compression can enable this reduction, but only when it is enabled.

Fortunately, here at WP Engine we enable GZIP compression by default. What’s more, we offer outstanding resources to help you learn how to build an outstanding digital experience for your visitors!

Get started

Build faster, protect your brand, and grow your business with a WordPress platform built to power remarkable online experiences.