BYTESLIP ← Back
Knowledge Base // Tutorial 119

Browser Caching and Brotli/Gzip Compression via .htaccess on Shared Hosting

Difficulty: Medium ~30 min .htaccess · Apache · LiteSpeed

Faster repeat visits and smaller pages help both readers and Core Web Vitals, and on shared hosting the switches are in .htaccess. This guide checks what your server already sends, then adds one block that sets Cache-Control by file type, plus compression for hosts that don’t do it already. We tested the block on Apache 2.4.71 and checked every header with curl.

Audit, back up, cache and compress Four tiles: curl audit, backup .htaccess, Cache-Control by type, and verify Brotli or gzip. CURL AUDIT BACKUP .HTACCESS CACHE-CONTROL BY TYPE BR / GZIP VERIFY
Safety // Read First

Warnings

ImportantA broken .htaccess can take the whole site down with a 500 error. Back it up before every edit, and keep the cPanel File Manager open so you can restore it. Long cache times on files that change mean visitors keep the old copy, so only use long times for files you rename or version when you change them.

Checklist // Before You Start

What You Need

How this was tested: Built Apache 2.4.71 from source on Linux with mod_headers, mod_mime, mod_deflate and mod_brotli, set AllowOverride All, and served a test site with the block below. Results: HTML, feed.xml and robots.txt got no-cache; CSS got 1 week, images 30 days, and woff2 1 year immutable. br, gzip requests got Brotli, gzip requests got gzip, and plain requests got uncompressed files. A conditional request returned 304. On Nov 12, 2026, we also ran the audit loop against this site: LiteSpeed was already sending Brotli for HTML, CSS and JS.

Walkthrough // Steps 01–05

Audit → back up → cache rules → compression → verify

Every command works in Termux. Replace example.com with your domain.

  1. Step 1: See what your server sends now

    for u in / /tutorials/ /css/site.css /img/logo.png; do
      echo "== $u"
      curl -sI -H 'Accept-Encoding: br, gzip' "https://example.com$u" |
        grep -iE '^(cache-control|expires|content-encoding|content-type|last-modified|etag)'
    done

    Read it like this: no cache-control on HTML means browsers may apply heuristic caching. MDN says they’re allowed to guess how long to keep the file, so set it explicitly. content-encoding: br or gzip means compression is already on. On LiteSpeed, the docs say Brotli is on by default for HTTPS static files, so you may get to skip Step 4.

  2. Step 2: Back up .htaccess

    In cPanel File Manager, open public_html, select .htaccess, and click Copy. Or over SSH:

    cp .htaccess .htaccess.bak-$(date +%Y%m%d)

    If anything breaks, copy the backup back over .htaccess. The change is immediate.

  3. Step 3: Add Cache-Control rules by file type

    Paste this whole block at the end of .htaccess. Section 1 makes sure AVIF, WebP and woff2 get the right content type. Section 2 sets caching, and Section 3 handles compression (next step).

    # ---- Byteslip: caching + compression (Apache 2.4 / LiteSpeed) ----
    # 1) Make sure modern types are labeled correctly
    <IfModule mod_mime.c>
      AddType image/avif .avif
      AddType image/webp .webp
      AddType font/woff2 .woff2
      AddType application/rss+xml .rss
    </IfModule>
    
    # 2) Cache-Control by file type
    <IfModule mod_headers.c>
      # HTML, feeds, sitemaps, robots: always revalidate so new posts show up
      <FilesMatch "\.(html?|xml|txt)$">
        Header set Cache-Control "no-cache"
      </FilesMatch>
      # CSS and JS: 1 week (bump ?v= in your HTML when you change them)
      <FilesMatch "\.(css|js|mjs)$">
        Header set Cache-Control "public, max-age=604800"
      </FilesMatch>
      # Images: 30 days
      <FilesMatch "\.(avif|webp|jpe?g|png|gif|svg|ico)$">
        Header set Cache-Control "public, max-age=2592000"
      </FilesMatch>
      # Fonts almost never change: 1 year
      <FilesMatch "\.(woff2?)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
      </FilesMatch>
    </IfModule>
    
    # 3) Compression for text (skip if curl already shows content-encoding)
    <IfModule mod_brotli.c>
      AddOutputFilterByType BROTLI_COMPRESS text/html text/css text/plain text/javascript application/javascript application/json application/xml application/rss+xml image/svg+xml
    </IfModule>
    <IfModule mod_deflate.c>
      AddOutputFilterByType DEFLATE text/html text/css text/plain text/javascript application/javascript application/json application/xml application/rss+xml image/svg+xml
    </IfModule>
    # ---- end Byteslip block ----

    Why these numbers: no-cache still lets browsers store the file but makes them check with the server first. When nothing changed, that check is a fast 304 Not Modified, so readers and crawlers see new posts right away. CSS and JS get one week because they’re linked with a ?v= number that you bump when you change them. web.dev recommends a long max-age only for URLs that change when the content changes. Fonts rarely change, so they get a year with immutable.

  4. Step 4: Compression only if it’s missing

    Section 3 turns on Brotli and gzip on Apache through mod_brotli and mod_deflate. The <IfModule> wrappers mean it does nothing where a module is missing. On LiteSpeed, compression is set at the server level and Brotli is on by default for secure static pages. If your Step 1 output already showed content-encoding: br, you can delete section 3. Never compress images or fonts this way, because they’re already compressed.

  5. Step 5: Verify headers and revalidation

    Run the Step 1 loop again and check that every line has the cache-control you expect. Then test that HTML revalidation works:

    lm=$(curl -sI https://example.com/ | tr -d '\r' | grep -i '^last-modified' | cut -d' ' -f2-)
    curl -s -o /dev/null -w '%{http_code}\n' -H "If-Modified-Since: $lm" https://example.com/

    304 means the browser can reuse its copy without downloading the page again. If you get 500 on any page, restore the backup and check for a missing </FilesMatch> or </IfModule>.

Pro Tip

Version the file, not the cache

Questions // FAQ

FAQ

Expires or Cache-Control?

MDN’s Expires page says that if a response has Cache-Control with max-age, Expires is ignored. If your host already adds Expires, the Cache-Control values from this block are the ones that count.

Will no-cache on HTML slow my site?

Only a little. The browser sends a small check, and an unchanged page comes back as a 304 with no body. It’s the safest setting for pages you update.

Does LiteSpeed read these Apache rules?

LiteSpeed says it’s Apache-compatible and reads .htaccess. Its own security-headers guide uses Header set inside IfModule mod_headers.c in .htaccess, and its compatibility list includes mod_deflate. Its unsupported list includes Header edit and mod_negotiation, which this block doesn’t use. Still, check with curl after you upload, as in Step 5.

Why not cache HTML for a day too?

Returning visitors and crawlers would keep seeing the old page, including old hub pages that don’t list your newest guide yet. no-cache keeps HTML fresh, and the 304 check keeps it cheap.

Keep Reading
Sources // Verified

Official docs

Cache-Control directives, heuristic caching, no-cache and immutable: MDN Cache-Control. Versioned URLs and long max-age: Prevent unnecessary network requests with the HTTP Cache. Apache modules: mod_headers, mod_deflate, mod_brotli, mod_mime. LiteSpeed .htaccess support, Brotli default and unsupported items: LiteSpeed Web Server configuration, LiteSpeed security response headers and LiteSpeed introduction. Expires ignored when max-age is set: MDN Expires. Hidden files in File Manager: cPanel File Manager.

← All Tutorials Byteslip Apps