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?
The 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.
Compressed files transfer faster than uncompressed files do. This helps to improve website performance, loading speeds, and Search Engine Optimization (SEO).
How to Check and See If GZIP Compression Is Enabled
GZIP compression is popular, and many WordPress hosting platforms enable it by default. If you are unsure whether GZIP compression has been enabled, you can check using third-party solutions or browser devtools.
If you do not have GZIP enabled, you will generally see warnings in tools such as GTmetrix. You can also use a solution like Varvy’s GZIP Tool to check and see whether your website is using GZIP compression.
It is also possible to see whether GZIP compression is enabled using browser devtools. When enabled, the response header will include “content-encoding: gzip”:
To find the response header in Chrome, you will need to open the 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
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, 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_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
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.
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:
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/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/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 no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
To enable mod_gzip instead, add:
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
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$
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 boost page speed and overall performance. GZIP compression can enable this reduction, but only when it is enabled.