How to Enable Gzip Compression in .htaccess for WordPress

How to Enable Gzip Compression in .htaccess for WordPress

If a speed test flags “enable text compression” or “compress components with gzip,” your server is sending HTML, CSS, and JavaScript uncompressed. Turning on gzip is one of the highest-impact, lowest-effort speed wins, often cutting text file sizes by around 70%.

What gzip does

Gzip compresses text-based responses before sending them to the browser, which unpacks them on arrival. Smaller transfers mean faster page loads, especially on slow connections. It applies to text: HTML, CSS, JavaScript, JSON, SVG, and XML. It does not help already-compressed formats like JPEG, PNG, WebP, or fonts in woff2, so those are left out.

Add the rules to .htaccess

  1. Open the .htaccess Gzip Compression Generator and pick the content types to compress (the defaults cover the important ones).
  2. Copy the generated mod_deflate block.
  3. Paste it into the .htaccess file at your site root, then save.

The rules are built in your browser; nothing is uploaded.

A typical block looks like this

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/css application/javascript
  AddOutputFilterByType DEFLATE application/json image/svg+xml
</IfModule>

It only takes effect if Apache has mod_deflate enabled, which nearly every host does.

Verify it is working

After saving, reload your page and check the response headers for your CSS or JS files. You are looking for:

content-encoding: gzip

If you see it, compression is on. You can also re-run your speed test; the compression warning should be gone.

Nginx instead of Apache?

.htaccess is an Apache feature. If your site runs on Nginx, use the Nginx Gzip Configuration Generator and add the block to your server config instead.

Pair it with caching

Compression and browser caching are the two halves of the same speed fix. Once gzip is on, set far-future cache headers for your static assets with the .htaccess Browser Caching Generator.

Gzip is a five-minute change with a real payoff. Generate the block, paste it into .htaccess, confirm the content-encoding: gzip header, and move on to caching.

← All posts