Enable Gzip compression in WordPress with htaccess

By Abhineet Mittal | WordPress
Published on: August 15, 2019
Last updated on: August 17, 2019

With the advancement of web technologies, you have ready-to-use frameworks for any functionality you want to add to your website or Wordpress blog. Just include these framework files to your website and add its features through their easy to use API.

For example, if you want readymade styling for your website, you can use CSS frameworks like Bootstrap or Foundation. Similarly, a lot of Javascript libraries are available to add advanced features to your website, without needing you to code complex functions.

Using these frameworks, developers can create fully functional websites in a short time. All they have to do is to include the framework files to their HTML page, then use its functions to implement the desired functionality.

Similarly, these days, people are more oriented towards visual data. It has become necessary to add a lot of images and videos to their content to attract and engage users. Many times, they also include custom-developed CSS and JS files in addition to all the other resources.

Though this makes the websites better in terms of functionality and aesthetics, adding all these files makes the webpage very heavy and negatively affects the website performance.

In a lot of Wordpress themes, each page load requires dozens of additional scripting and styling files in addition to the already included media files.

When a user opens such a website in their browser, the browser immediately starts downloading all the resources from the requested web page and render it for the user. It may include stylesheets, script files, images, videos, etc.

Each request to such files increases the page load time as the user has to wait for the resources to get downloaded. If you have multiple files with huge sizes, it adversely affects your site performance.

Now, if the user is on a high-speed internet connection, this may not be a problem at all. But, you cannot assume that all your users are accessing your website through the best internet connections.

As a simple Wordpress user, you may have downloaded or purchased an excellent theme for your blog. You may like the functionality of that theme, but the extra file requests are slowing down your website.

You may not know how to optimize the theme through coding. But, you can optimize your blog for better performance through some simple tweaks at the server level.

Two things can be done to improve your Wordpress blog performance. They are browser caching and Gzip compression. You can do this at the server level by simply adding some configuration code to .htaccess file in your website root folder.

Browser Caching ensures that your static resources are cached by the user browser to avoid multiple requests of the same file on different pages. The user will save on bandwidth, and the site load speed will improve.

I have already written a post about Browser Caching where I have provided the required configuration.

Let's take a look at Gzip compression in this post. Gzip compression speeds up your Wordpress website by compressing the resources at the server level using simple .htaccess configuration in Apache server.

Gzip compresses the files to reduce payload. It even compresses the minified CSS and JS files to smaller sizes.

Just add the following code to your .htaccess file.

# BEGIN Compressing

# 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

# END Compressing

This code ensures that Apache will compress all the files using Gzip before sending them to the user.

Once you implement this code, check your page speed using Google Page Speed Insights tool. You will notice the improvement in your page speed score.

Even if you are not facing website speed issues, you must implement Browser Caching and Gzip Compression on your website. It is recommended by Google & widely supported by most browsers.

Share this:
FacebookTwitterWhatsapp

Abhineet Mittal
Abhineet is an entrepreneur by profession and programmer by heart. He loves to write about WordPress. He is the Founder of popular Digital Publishing Platform, Sahityapedia.