Learn how to enable Gzip compression using your .htaccess file to reduce file sizes, improve website loading speed, and boost overall performance in this step-by-step guide.
Your pages ship a lot of text: HTML, CSS, JavaScript. That text compresses well, and gzip does it before the bytes leave your server. Turn it on and a typical CSS or JS file drops 60 to 80 percent on the wire. Here’s how to switch it on in .htaccess on Apache.
One thing to check first: a lot of hosts and every serious CDN (Cloudflare, Fastly, most managed WordPress hosts) already do this for you. Run your site through the tools in Step 3 before you touch anything. If compression already shows up, you’re done, leave the file alone.
Step 1: Check if mod_deflate is Enabled
Gzip on Apache is handled by mod_deflate. The old mod_gzip module is legacy and isn’t present on modern Apache, so ignore any guide that reaches for it. To confirm mod_deflate is loaded, drop this in your .htaccess:
<?php
<IfModule mod_deflate.c>
# mod_deflate is enabled
</IfModule>Anything inside those tags only runs when the module is active. If your rules do nothing, mod_deflate isn’t loaded, and that’s a call to your host, not something you can force from .htaccess.
Step 2: Add the Gzip Compression Code
Once mod_deflate is confirmed, add the block below. It tells Apache which content types to compress and carries a few guards for ancient browsers:
<?php
# 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-aliveNotice the list is all text formats: markup, styles, scripts, fonts, SVG. That’s on purpose. JPEGs, PNGs, WebP, and most video are already compressed, so running them through gzip burns CPU for no gain. Leave them off the list. The keep-alive block lets several requests reuse one connection, which shaves a little latency on top.
Step 3: Test Your Website
Don’t trust it because you saved the file. Confirm it. Run your URL through GTmetrix or Google PageSpeed Insights, or check the Content-Encoding response header in your browser’s Network tab. If it says gzip, you’re compressing.
One honest caveat: gzip is the safe, universal option, but it isn’t the fastest anymore. If your host or CDN offers Brotli, it usually squeezes text 15 to 20 percent smaller and most browsers support it. Use it when you can. And whichever you pick, run only one, don’t stack gzip behind a CDN that’s already compressing, or you pay twice for the same job.


