To enable gzip compression and browser caching on Apache or LiteSpeed hosting, add two short blocks to the .htaccess file in your site's main folder. Gzip shrinks text files on the way to the visitor, and caching tells the browser to keep your CSS, JavaScript and images, so it does not download them again. Both are free and take about ten minutes.

What do gzip and browser caching do?

Gzip is a way to pack files smaller before the server sends them, much like a zip file. The browser unpacks them in a blink. It works best on text files: HTML, CSS, JavaScript, JSON and SVG. Google's old PageSpeed guide says gzip can cut the size of a response by up to 90%. Brotli is a newer method that often packs files even smaller. Many hosts offer both.

Browser caching means the browser saves a copy of a file on the visitor's device. On the next page or the next visit, it uses that copy instead of asking your server again. Your server tells the browser how long to keep each file with a header called Cache-Control. A header is a short note the server sends along with every file.

Together they make pages lighter and repeat visits faster. That helps your readers on slow phones and your speed scores, which our guide to Core Web Vitals for beginners explains.

Where is the .htaccess file?

The .htaccess file is a settings file that Apache and LiteSpeed servers read from your site's folders. Most shared hosts with cPanel use one of these two servers.

  1. Log in to cPanel and open File Manager.
  2. Open public_html, the main folder of your site.
  3. Click Settings and tick Show Hidden Files. Files that start with a dot are hidden by default.
  4. Right-click .htaccess and choose Edit. If there is none, create a new file with that exact name.

Before you change anything, download a copy of the file. One typo can break the whole site with a "500 Internal Server Error". With a copy, you just upload the old file again. Our guide to website backup basics explains why this habit pays off.

How do I enable gzip compression in .htaccess?

Paste these three lines at the end of your .htaccess file. They use Apache's mod_deflate, the part of the server that does the packing:

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

The IfModule lines are a safety net. If your host does not have that part of the server, Apache skips the block instead of showing an error. The list in the middle names the file types to pack. JPEG, PNG and WebP images are left out on purpose, because they are already packed. Packing them again wastes server time and saves almost nothing.

Some cPanel hosts also have a button for this. Look for Optimize Website under Software and choose to compress all content.

How do I turn on browser caching?

Paste this block below the gzip lines. It uses mod_expires, which sets how long each file type may stay in the browser:

  • <IfModule mod_expires.c>
  • ExpiresActive On
  • ExpiresByType text/css "access plus 1 year"
  • ExpiresByType application/javascript "access plus 1 year"
  • ExpiresByType image/webp "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"
  • </IfModule>

Apache's docs say these lines send both an Expires header and Cache-Control: max-age. One year is 31,536,000 seconds, so the browser gets max-age=31536000. That is the value web.dev suggests for files whose content never changes at the same address.

Notice that HTML pages are not on the list. Your pages change when you post or edit, so you want visitors to get the fresh copy every time.

What if I change a cached file?

A visitor's browser may keep the old CSS file for a year. So when you edit a CSS or JavaScript file, change its address. The easy way is a version number at the end of the link in your page: style.css?v=2, then ?v=3 next time. The browser sees a new address and downloads the new file.

The free .htaccess Caching and Gzip Generator builds both blocks for you, with the file types you pick.

What about Nginx, Cloudflare and Bloghints CMS?

  • Nginx: it does not read .htaccess. Ask your host, or add gzip on; and gzip_types text/css application/javascript application/json image/svg+xml; to the server block. For caching, add expires 1y; inside a location block for your CSS, JS and image files.
  • Cloudflare: if your site runs through Cloudflare, it already compresses text files for visitors. You do not need to change anything there. Your own server's cache headers still matter.
  • Bloghints CMS: the .htaccess file that comes with the CMS already turns on compression and sends one-year caching for CSS, JS, images and fonts. Check that your host did not replace it.
  • WordPress: many cache plugins add the same rules for you. Use one plugin, or the .htaccess blocks above, not both.

How do I check that it works?

  1. Open your page in Chrome and press F12 to open DevTools.
  2. Go to the Network tab and reload the page.
  3. Click your page's name at the top of the list, then open Headers.
  4. Under Response Headers, look for content-encoding: gzip or br (Brotli). Either one means compression works.
  5. Click a CSS file or an image and look for cache-control: max-age=31536000.

If neither header shows, your host may not allow these modules, or another rule may override them. Ask your host's support. On my current host, the AI support assistant helped me turn on server features such as cURL right away, so asking is quick these days.

A personal note: I once paid hundreds of dollars a year for hosting sold as many times faster, and I felt no real difference. Settings like these cost nothing. If you are thinking about a pricier plan for speed, try these free steps first. Our guide on how to choose web hosting covers what is worth paying for.

Now you know how to enable gzip compression and browser caching: two .htaccess blocks, a backup first, and a quick look at the headers. Re-run the free Website SEO Checkup to confirm the compression and caching warnings are gone.

Frequently asked questions

Is Brotli better than gzip?

Brotli usually makes text files a bit smaller than gzip, and most modern browsers support it. If your host offers Brotli, turn it on. Gzip is still a good fallback for older browsers.

Will browser caching show visitors an old version of my site?

Not your pages, because the rules above do not cache HTML for long. Only CSS, JavaScript and images stay cached. Change the file name or add a version number when you edit them.

Can gzip slow down my server?

Packing files takes a little server work, but on a normal blog it is tiny next to the time saved sending smaller files. For almost every site, it is worth turning on.