How we enable gzip compression through htaccess?
Looking for a way to speed up your Apache-based web server? One option is to enable gzip compression through the .htaccess file.
Gzip compressions reduce file size that in-turn increases website speed.
To know how to optimize the website loading time using the .htaccess file, read on.
What is gzip compression?
Gzip is a method that makes files smaller in size, that is, it simply compresses the files. But does compressing files helps in increasing website speed? Yes. Let’s see how it is possible.
When a visitor makes a request to access a website, the server sends the corresponding file. If the file is larger, say 1MB, it takes around 5 seconds to reach the visitor’s browser.
At the same time, if the server compresses this 1MB file to 50KB using a compression method like gzip, it reaches its destination within milliseconds.
That is, smaller files have a better transmission in a network.
Hence, compression improves the speed of the website and reduces the load time. Because the visitor will need to download smaller web files when compared to the original ones.
Next, let’s get into more detail on how to enable gzip compression in an Apache-based web server using a .htaccess file.
How to enable gzip compression using .htaccess?
The .htaccess file is nothing but the configuration file in Apache-based web servers.
We, the webmasters actually use two different mods to enable the gzip compression: mod_gzip and mod_deflate.
Mod_gzip enables gzip compression and mod_deflate compresses the output from the server before it is being sent to the visitor.
So, both do the same thing. Generally, we recommend using mod_deflate since it is easier to use.
We can enable these by adding a code in the .htaccess file.
To enable mod_deflate, our Support Engineers add the below code in .htaccess file
And we add the below code to enable mod_gzip,
Common errors related to gzip compression
Recently, one of our customers approached us telling us that, the gzip compression was not working. He received the below error in the plugin.
Gzip is not working properly:
He enabled deflate gzip using the .htaccess file.
Let’s see how our Support Engineers fix this problem.
1. Checked permissions
Our Support Engineers started troubleshooting the problem by checking the permission of .htaccess file.
On checking, we found that the permission of the file was set to 644 which was correct. So we moved on to troubleshoot further.
2. Checked extension availability
Then, we further accessed the php.ini file to see if the zlib extension is set or not. We saw it was not active. So we added the below code in php.ini.
zlib.output_compression = On
Sometimes this may fix the error. But in most cases, it won’t. So we checked further.
3. Checked Apache module installation
Sometimes, the mod_gzip or mod_deflate modules will not be set on the server which causes errors. So, we checked for the availability of the module using the below command:
httpd -M | grep deflate
If mod_deflate is available, we can see
Since we could not see the module in the server, we enabled it.
Finally, this fixed the error.
[Need any assistance in setting the gzip compression and fixing its errors? – We’ll help you.]
In short, the gzip compression helps in reducing the file size and making the website load faster. Today, we saw how our Support Engineers enable gzip compression using htaccess and fix its errors.