Dev Notes

How to Enable Gzip and Brotli Compression and Browser Caching with .htaccess

Gzip, Brotli & Browser Caching: Copy-paste .htaccess rules for faster pages
Dev Notes August 13, 2019 Tamir Khan No Comments

Quick answer

To speed up a site on Apache or LiteSpeed, enable compression and browser caching in .htaccess: use mod_deflate (Gzip) or mod_brotli (Brotli) to compress HTML, CSS, JavaScript, JSON and SVG, and use mod_expires or Cache-Control headers to let browsers keep static files for up to a year. Test with curl -I -H “Accept-Encoding: br, gzip” and check for a content-encoding header.

Key takeaways

  • Compression typically shrinks HTML, CSS and JavaScript by 60–80%.
  • Brotli compresses text better than Gzip; serve Brotli where available and Gzip as fallback.
  • Don’t compress images, video or fonts that are already compressed (JPEG, WebP, WOFF2).
  • Cache static files for a long time and change the file name or version when they change.
  • If you use Cloudflare or a managed host, compression may already be on; check before adding rules.

Two server settings make almost every website faster with a few lines of configuration: compression, which shrinks text files before sending them, and browser caching, which lets returning visitors reuse files instead of downloading them again. Both are flagged by PageSpeed Insights (“Enable text compression”, “Serve static assets with an efficient cache policy”). Here is how to enable them in .htaccess on Apache and LiteSpeed servers.

What is Gzip and Brotli compression?

When a browser requests a page, it tells the server which compression formats it understands (Accept-Encoding: gzip, br). The server compresses text responses before sending, and the browser decompresses them instantly. Brotli, developed by Google, usually produces smaller files than Gzip for HTML, CSS and JavaScript, and all modern browsers support it over HTTPS.

How do I check whether compression is already enabled?

curl -sI -H "Accept-Encoding: br, gzip" https://example.com/ | grep -i content-encoding

content-encoding: br means Brotli is on; gzip means Gzip. No line means compression is off for that file. You can also check the Network tab in Chrome DevTools.

How do I enable Gzip compression in .htaccess?

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml
  AddOutputFilterByType DEFLATE application/javascript text/javascript application/json
  AddOutputFilterByType DEFLATE application/xml application/rss+xml application/ld+json
  AddOutputFilterByType DEFLATE image/svg+xml font/ttf font/otf application/vnd.ms-fontobject
  Header append Vary Accept-Encoding
</IfModule>

How do I enable Brotli compression?

If your Apache server has mod_brotli (Apache 2.4.26+), add this above the Gzip block so Brotli is preferred:

<IfModule mod_brotli.c>
  AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/css text/xml
  AddOutputFilterByType BROTLI_COMPRESS application/javascript text/javascript application/json
  AddOutputFilterByType BROTLI_COMPRESS application/xml application/rss+xml image/svg+xml
</IfModule>

LiteSpeed servers handle Brotli and Gzip at server level, usually with no .htaccess changes. Check with your host or in LiteSpeed Cache settings.

Need a website that turns visitors into enquiries?

My team builds fast, secure, SEO-ready websites and web apps for growing businesses.

Get a website quote WhatsApp me

How do I enable browser caching in .htaccess?

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 month"
  ExpiresByType text/html "access plus 0 seconds"
  ExpiresByType text/css "access plus 1 year"
  ExpiresByType application/javascript "access plus 1 year"
  ExpiresByType image/webp "access plus 1 year"
  ExpiresByType image/avif "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/svg+xml "access plus 1 year"
  ExpiresByType font/woff2 "access plus 1 year"
</IfModule>

<IfModule mod_headers.c>
  <FilesMatch "\.(css|js|webp|avif|jpe?g|png|svg|woff2)$">
    Header set Cache-Control "public, max-age=31536000, immutable"
  </FilesMatch>
</IfModule>

HTML is set to not be cached by the browser so visitors always get fresh pages. For CSS and JavaScript cached for a year, make sure file names change when the content changes. WordPress adds ?ver= numbers automatically, and most cache plugins create new file names when minifying.

Where should these rules go?

  • In the .htaccess file in your site’s root folder, above # BEGIN WordPress on WordPress sites.
  • Back up the file first. A typo causes a 500 error; restore the backup to fix it.
  • If you have access to the main Apache configuration, putting rules there is slightly faster than .htaccess.

What about Nginx and Cloudflare?

Nginx doesn’t read .htaccess. Add to the server block:

gzip on;
gzip_types text/css application/javascript application/json image/svg+xml text/xml;
location ~* \.(css|js|webp|avif|jpg|jpeg|png|svg|woff2)$ {
  expires 1y;
  add_header Cache-Control "public, immutable";
}

Cloudflare compresses responses with Brotli and Gzip automatically and lets you set Browser Cache TTL under Caching. Your origin rules still matter for the cache headers Cloudflare passes on.

What else improves page speed?

Compression and caching are only part of the picture. For WordPress, also look at hosting, page caching, images, plugins and scripts. See 15 fixes to speed up WordPress.

Frequently asked questions

Is Brotli better than Gzip?

For text files, Brotli usually produces smaller files than Gzip at similar speed, so pages load faster. Serve Brotli to browsers that support it and keep Gzip as a fallback.

Should I compress images with Gzip?

No. JPEG, PNG, WebP, AVIF and WOFF2 are already compressed. Gzipping them wastes server CPU with little or no size gain. Compress text formats such as HTML, CSS, JavaScript, JSON and SVG.

Why does PageSpeed Insights still say enable text compression?

The rules may not apply because the module is not installed, a CDN is stripping headers, or the files come from a third-party domain you don’t control. Test the specific URLs flagged with curl.

How long should browser caching be?

A year (31536000 seconds) for versioned static files like CSS, JavaScript, images and fonts, and no long caching for HTML pages that change.

Tamir Khan: business growth and digital strategy consultant in Mumbai. Since 2011 he has built websites, SEO, Google Ads, cloud and automation systems for businesses in India, the UK, Canada, the USA and the UAE. Strategy comes from Tamir; delivery is by the NexGeno team. About Tamir · LinkedIn

Last updated: 25 November 2026

Related Posts

Quick Links

Newsletter

I am the Founder and Chief Technology Officer at NexGeno Technology Pvt Ltd. With 12+ years of experience, I specialize in digital strategy, business automation, AI integration, web development, and marketing.I've worked with startups, SMEs, and global clients, offering tailored strategies that deliver measurable results.

© 2025 Tamir Khan - All Rights Reserved | Managed by NexGeno