256 Kilobytes

How to use GZip compression on your website

Articles in Server-Side | By Hash Brown


GZip is a form of file compression that can be used by websites to reduce the size of the files being transferred to users. this decreases load time as well as reduces server load.

967 views, 0 RAMs, and 0 comments

In the quest for peak website performance you will come across GZip compression.

This is something that can be used by the majority of websites to help decrease load times of websites as well as use less server resources.

What is GZip compression?

GZip compression is a method of compressing files with the goal of faster network transfers to users. 

By making files smaller in size they download far quicker for your users, this means your website will load quicker on their end.

GZip is very common and often enabled by default on many CMS platforms however if you have recently run your website through a speed tool and they let you know that this wasn't enabled, this guide will show you how it's done.

GZip is also a file type, for the record we will be talking about using GZip to speed up websites. Not dealing with unzipping GZip files.

Why should you use GZip compression?

GZip Compression makes websites load faster for your users and reduces server load.

Everyone wants fast websites. GZip compression can reduce files sizes on transfers by 70%, it really can make a significant difference.

How does it work?

GZip compression makes files smaller. This means when a visitor loads your website instead of a 100kb file being downloaded they may download a 10kb file.

This is downloaded far quicker. On the users end inside the browser the file is decompressed in the browser and loads as if nothing happened.

This works in the same was a RAR file or .zip that you will have probably used in your limewire days.

How do I enable GZip compression?

Gzip is very easy to enable, it's done through the .htaccess dot file.

Step 1: Open your .htaccess file at your domains root

We need to edit this file, you can access it with FTP. If this file is not there you will need to create a file called ".htaccess".

.htaccess is a dot file we can use to enable certain features in Apache.

Step 2: Paste in this code

GZip compression works best on text based files such as .html, .php, .css, .js and so on. It's not a good idea to use Gzip on images, audio, video or other binary files. These are already compressed and compressing them twice can actually make the files larger and slower.

So, we stick to using GZip on text based files. This is the code snippet we need to add to .htaccess:

<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 handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*

This block of code is very simple. 

The ifModule block checks we have mod_gzip.c available to use.

<ifModule mod_gzip.c>

The next two lines then enabled it once we know it's there to be enabled.

mod_gzip_on Yes
mod_gzip_dechunk Yes

mod_gzip_item_include sets up a list of files and mimes we want to compress with GZip.

mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*

mod_gzip_item_exclude sets up a list of files we do not want to ever gzip, in this case all images and files that have already been compressed.

mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*

It's all simple stuff.

Step 3: Save your edited .htaccess and upload

Upload/save your .htaccess file. 

Step 4: Test

It's worth checking your website to make sure GZip was enabled correctly, there is a very handy online tool below just for that purpose:

Gzip FAQ

Why do we only GZip text based files?

If you're building your website properly files such as images and videos should be already compressed. GZip compression would work on these file types however it's not as effective as saving these files properly in the first place or letting a pluggin handle it.

Should I always use GZip?

Old browsers will struggle to render and deal with Gzip, if you are developing on legacy systems or your users are using dated software you should be aware of this issue and make sure it won't cause any issues.

How do I unzip a GZip file?

Use software such as 7zip.

How do I enable GZip on WordPress?

Add the above code to the .htaccess file and/or check your cache pluggin to see if it has the option to enable GZip compression. Most of them do.

How do I test if GZip is working?

There are online Gzip tools such as this.

Download more RAM. 🐏 ⨉ 0Posted by Hash Brown 3 years ago 🕓 Posted at 10 March, 2019 20:51 PM PDT

Read More:

Post a New Comment

Do you like having a good time?

Register an Account

You can also login to an existing account or reset your password. All use of this site is subject to the terms of service and privacy policy.

Read Quality Articles

Read some quality articles. If you can manage to not get banned for like five minutes, you can even post your own articles.

View Articles →

Argue with People on the Internet

Use your account to explain why people are wrong on the Internet forum.

View Forum →

Vandalize the Wiki

Or don't. I'm not your dad.

View Wiki →

Ask and/or Answer Questions

If someone asks a terrible question, post a LMGTFY link.

View Answers →

Make Some Money

Hire freelancers and/or advertise your goods and/or services. Hire people directly. We're not a middleman or your dad. Manage your own business transactions.

Register an Account