Gzip compression is a powerful technique for reducing file sizes, which helps improve website loading speed and overall performance. By enabling Gzip compression via your .htaccess file, you can minimize the size of your website files and speed up page load times for your visitors. In this tutorial, we’ll guide you step-by-step on how to enable Gzip compression using .htaccess.
Step 1: Check if mod_deflate is Enabled
First, you need to check if mod_deflate, the Apache module that handles file compression, is enabled on your server. You can do this by adding the following code to your .htaccess file:
<IfModule mod_deflate.c> # mod_deflate is enabled </IfModule>
If mod_deflate is enabled, the code inside the <IfModule> tags will execute. If nothing happens, this means mod_deflate is not enabled, and you should contact your hosting provider to activate it.
Step 2: Add the Gzip Compression Code
Once you’ve confirmed that mod_deflate is active, you can add the Gzip compression code to your .htaccess file. This code will tell the server which file types to compress and includes fixes for older browsers. Here’s the code:
# BEGIN Gzip compression htaccess <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> # END Gzip compression htaccess # BEGIN Connection keep-alive <ifModule mod_headers.c> Header set Connection keep-alive </ifModule> # END Connection keep-alive
This snippet compresses various file types, including HTML, CSS, JavaScript, and fonts. Compressing these files reduces the amount of data transferred, which speeds up your site. The browser-specific rules included in the code help ensure compatibility with older browsers that may not handle compression well.
Enabling keep-alive connections with the Header set Connection keep-alive directive allows multiple requests to be processed over a single connection, further reducing latency and improving performance.
Step 3: Test Your Website
After adding the Gzip compression and keep-alive settings to your .htaccess file, it’s essential to test your website to confirm everything is working correctly. Use tools like GTmetrix or Google PageSpeed Insights to analyze your website’s performance. These tools will show if Gzip compression is enabled and how it affects your page load speed.
If the configuration is correct, you should see a noticeable improvement in your website’s performance metrics, leading to faster load times and a better user experience.
Conclusion
Enabling Gzip compression via .htaccess is a simple yet effective way to enhance your website’s loading speed and overall performance. By following the steps outlined in this tutorial, you can efficiently compress your website’s files, resulting in quicker load times for your visitors. Always remember to test your site after making changes to your .htaccess file to ensure everything functions as expected and your optimizations work as intended.

