How to Enable GZIP Compression for WordPress

Gzip compression is a way to compress the files on your server, on this article we will explain how to set it.

To obtain fast load times on your WordPress site, decreasing the size of your pages is crucial. This can mean the difference between a site that loads in 1 second and one that feels like its crawling and taking ages. Enabling GZIP compression can help reduce the size of your webpage, which can significantly reduce the amount of time to download the resource, reduce data usage for the client and improve the time to first render of your pages.

All modern browsers support it and automatically negotiate GZIP compression for all HTTP requests.

Today we are going to share with you how to check and enable GZIP compression on your web server.

What is GZIP Compression?

GZIP is a file format and a software application used for file compression and decompression. GZIP compression is enabled server-side, and allows for further reduction in the size of your HTML, stylesheets, and JavaScript files. It will not work on images as these are already compressed in a different way. Some have seen up to 70% reductions due to compression. It is probably one of the easiest optimizations you can make when it comes to WordPress.

Compression is the process of encoding information using fewer bits. — Ilya Grigorik, Google

When a web browser visits a website it checks to see if the web server has GZIP enabled by seeing if the “content-encoding: gzip” response header exists. If the header is detected, it serves up the compressed and smaller files. If not, is serves up the uncompressed files. If you don’t have GZIP enabled, you will most likely see warnings and errors in speed testing tools such as GTmetrix.

GZIP Warning in GTmetrix

GTmetrix will show this warning and recommend to enable GZIP compression to reduce the transfer size of the static resources.

enable gzip compression warning in gtmetrix

How to Check if GZIP Compression is Enabled

GZIP is very common nowadays, and it is enabled by default on all the web hosting we recommend.

Best Web Hosting Services

GZIP is currently supported by most of the browser, as many have supported it for over 18 years. Here is a list of browsers that can handle the “content-encoding: gzip” HTTP response header:

  • Internet Explorer 5.5+ (July 2000)
  • Opera 5+ (June 2000)
  • Firefox 0.9.5+ (October 2001)
  • Chrome (soon after launch in 2008)
  • Safari (soon after first launch in 2003)

If you are running on another WordPress host though, you should always check to ensure it is enabled, as server admins often overlook this optimization. There are a couple quick ways to check for GZIP compression:

Check GZIP Compression Tool

The first and quickest way to check if GZIP compression is enabled on your site is to simply head over the varvy site free Check GZIP compression test. Simply input your website and click search. It will return the amount that was saved by compressing the page with GZIP. Or it will return an error letting you know GZIP isn’t enabled. As you can see in our test below, we saved 79%.

gzip compression saving 79%

How to Enable GZIP Compression

If you don’t have GZIP compression enabled, there are a couple ways you can go about enabling it on your webserver. Check below to see how the options you have.

Enable GZIP with WordPress Plugin

The first and one of the easiest is by using a caching plugin that supports enabling GZIP. WP Rocket for example adds GZIP compression rules in your .htaccess file automatically using the mod_deflate module. W3 Total Cache also has a way to enable this for you under it’s performance section. Even though these are plugins, this still relies on permissions to modify files on your webserver. If your caching plugin doesn’t have permission, you will need to ask your host or use a snippet of code below.

Enable GZIP on Apache

The second way to enable Gzip compression is by editing your .htaccess file.  Most shared hosts use Apache, in which you can simply add the code below to your .htaccess file. You can find your .htaccess file at the root of your WordPress site via FTP.

Important: Make sure mod_filter is loaded on your server, otherwise the AddOutputFilterByType directive will not work and could cause a 500 error. We recommend checking your error logs if you have any issues with the code below.

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

Ensure that you add it below the current contents of your .htaccess file. Example below:

Enable GZIP on NGINX

If you are running on NGINX, simply add the following to your nginx.conf file.

gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_vary on;
gzip_types text/plain text/css text/javascript image/svg+xml image/x-icon application/javascript application/x-javascript;

Enable GZIP on IIS

If you are running on IIS, there are two different types of compression, static and dynamic. We recommend checking out Microsoft’s guide on how to enable compression.

Conclusion

The loading speed of your website is one of the most important factor to get good placement in the search engines, if you don’t have GZIP compression enable on your server then don’t wait anymore and set it to boost your site loading speed and performance.

If your current web hosting don’t let you enable gzip compression then we recommend you to change your web hosting to one of the three we recommend in this article, the three hosting we recommend have it activated by default, no settings needed.

If you liked this article you can follow us on Twitter and Facebook.

Add Comment