WordPress

htaccess Browser Caching in 2026: The Rules, Plus How Cloudflare Changes Them

By , Lead SEO & Co-founder 7 min read
A storage shelf of glowing cache modules near the front of a server room at night, a short cyan path from them to an exit door and a long dim path to the back racks, a crimson timer light on the shelf and a laptop on a cart

htaccess browser caching tells a visitor's browser to keep your images, styles and scripts for a while, so the second page they open loads from their own device instead of your server. Many sites still run rules copied from tutorials written years ago: one day for HTML, a week for CSS, no WebP or AVIF, and a Vary: User-Agent line that quietly sabotages caching.

Here's the 2026 version: a year for versioned CSS, JavaScript and fonts, several months for images, and HTML left to your page cache. Then the part most guides skip. If Cloudflare or another CDN (a network of servers that keeps copies of your site close to visitors) sits in front of your site, it reads these headers too, and sometimes overrides them.

If you'd rather have someone set this up and check every header, describe your setup in the speed questionnaire and I'll reply with a plan and a fixed quote within one business day.

How browser caching works, in plain English

When your server sends a file, it can attach a note saying how long the browser may reuse it. There are two versions of that note:

  • Expires: a date. "Good until March 3."
  • Cache-Control max-age: a number of seconds. "Good for 31,536,000 seconds" (one year).

Apache's mod_expires module sets both headers at once. Its docs also say that for any other Cache-Control setting, like immutable, you need the Header directive from mod_headers. So a modern setup uses both modules.

The catch is obvious once you say it out loud: if the browser keeps your logo for a year, and you change the logo, returning visitors see the old one for a year. The fix is to change the file's URL whenever its content changes. web.dev's caching guide recommends a one-year max-age only for versioned URLs, and no-cache (keep a copy but check with the server first) for HTML.

WordPress already does half the work. When plugins and themes load a script or stylesheet, WordPress adds the version number to the URL as a query string for cache busting, like style.css?ver=6.8.3. Update the plugin and the URL changes, so browsers fetch the new file.

The 2026 htaccess browser caching rules

Put this in the .htaccess file in your site's root folder, outside the # BEGIN WordPress block, so WordPress doesn't overwrite it. Back up the file first.

.htaccess / Apache
# Browser caching for WordPress on Apache 2.4 or LiteSpeed (2026 version).
# Images and media: 6 months. CSS, JS and fonts: 1 year, marked immutable.
# Could break: a file you replace under the same name (logo.png, a hand-edited
# style.css) can stay stale for returning visitors. See "the one trap" below.

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/avif "access plus 6 months"
    ExpiresByType image/webp "access plus 6 months"
    ExpiresByType image/jpeg "access plus 6 months"
    ExpiresByType image/png "access plus 6 months"
    ExpiresByType image/gif "access plus 6 months"
    ExpiresByType image/svg+xml "access plus 6 months"
    ExpiresByType image/x-icon "access plus 6 months"
    ExpiresByType video/mp4 "access plus 6 months"
    ExpiresByType text/css "access plus 1 year"
    ExpiresByType text/javascript "access plus 1 year"
    ExpiresByType application/javascript "access plus 1 year"
    ExpiresByType font/woff2 "access plus 1 year"
</IfModule>

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

A few choices in there are deliberate.

No HTML rule. Old snippets cached HTML for a day, which means visitors could see yesterday's prices or a deleted page. In 2026, most WordPress sites have a page cache (from the host or a caching plugin) that manages HTML better. Let it.

No ExpiresDefault. A catch-all rule also hits things you don't want cached, like REST API responses. Name the types you mean.

CSS, JS and fonts matched by extension too. Some servers still send fonts with a generic file type, so the FilesMatch block catches them by file name. Header set replaces the header mod_expires added, and the mod_headers docs confirm set replaces any previous header with the same name.

immutable. MDN defines it as a promise that the file won't be updated while it's fresh, so the browser doesn't even re-check on reload. Only use it when your CSS and JS URLs change whenever the content does.

Lines to delete from old snippets

If you're replacing an older block, remove these:

  • Header append Vary User-Agent. It tells caches to store a separate copy for every browser version, which wrecks CDN and browser caching.
  • ExpiresByType text/html "access plus 1 day" or similar.
  • BrowserMatch lines for Internet Explorer and Netscape 4. They're older than most of your customers' phones.
  • Any <Files> rules using Order allow,deny. That's Apache 2.2 syntax.

If your site shows a 500 error right after saving, a typo or a missing module is the usual cause. My htaccess 500 error guide walks through the quick undo.

The one trap: files you replace in place

Long caching is safe for anything WordPress versions. It's risky for files you overwrite by hand with the same name:

  • A child theme's style.css edited directly, if your theme doesn't change its version number.
  • A logo or PDF re-uploaded by FTP under the old name.

The simple fix: give the new file a new name (logo-2026.png), or bump the version in your child theme's header after editing. You can purge a CDN cache, but there's no button to purge your visitors' browsers.

How these rules interact with a CDN

Here's where 2026 setups differ from the old tutorials. With Cloudflare in front of your site, three things happen.

1. Cloudflare reads your headers by default. Its docs say it honors the cache expiration set in your Expires and Cache-Control headers when Browser Cache TTL is set to "Respect Existing Headers." But the same page notes two exceptions: if your header asks for less time than the Cloudflare setting, or sends none at all, Cloudflare overrides it. Its default Browser Cache TTL is 4 hours.

My advice: set Browser Cache TTL to Respect Existing Headers, so your .htaccess stays the single source of truth. Two systems arguing about cache times is how you end up debugging a stale logo at 11 p.m.

2. Cloudflare doesn't cache your HTML unless you tell it to. By default, it does not cache HTML or JSON, and it decides what to cache by file extension (CSS, JS, images, fonts and so on). It also skips anything marked private, no-store, no-cache or max-age=0. So your static files get cached at the edge with the lifetimes from your rules, and HTML keeps coming from your server and page cache.

3. CDN cache and browser cache are separate. If you need the CDN to keep a file for a different time than browsers, Cache-Control has s-maxage, which MDN describes as the lifetime for shared caches like CDNs. It overrides max-age for them only. Cloudflare's own Edge Cache TTL setting does a similar job without touching your headers.

On other setups the idea is the same. LiteSpeed servers read .htaccess directly, and host-level caches like Varnish or Nginx usually have their own rules. Check what's in front of Apache before assuming your headers are the last word.

How to check it worked

Run this from any terminal, using a real CSS file from your site:

Terminal
# Shows the caching headers for one file. Read-only.
curl -sI "https://example.com/wp-includes/css/dist/block-library/style.min.css" \
  | grep -i -E "cache-control|expires|cf-cache-status|vary"

You want to see max-age=31536000, immutable and no Vary: User-Agent. Behind Cloudflare, cf-cache-status: HIT on a second request means the edge cached it too.

The old Leverage browser caching warning lives on in Lighthouse's documentation as Serve static assets with an efficient cache policy. Third-party files (analytics, chat widgets, fonts from another domain) will still show up there. You can't set headers for other people's servers, so don't chase those.

Final Thoughts

Good htaccess browser caching in 2026 is short: long lifetimes for files whose URLs change when they change, sensible months for images, and HTML left to the page cache. Add a CDN and set it to respect your headers, so there's one place to look when something's stale. Caching is one piece of page speed. For the rest, see my plain-English take on Core Web Vitals for small businesses.

htaccess browser caching FAQs

How do I set up browser caching in .htaccess?

Add mod_expires rules that set lifetimes by file type (six months for images, a year for CSS, JavaScript and fonts) and a mod_headers rule that adds immutable to versioned files. Place the block outside the WordPress section of .htaccess so updates don't overwrite it. Then check the Cache-Control header with curl or your browser's developer tools to confirm it's being sent.

How long should I cache CSS and JavaScript?

One year is standard for files whose URL changes when the content changes, which covers most WordPress plugin and theme files thanks to the ?ver= query string. If you edit a CSS file directly without changing its version or name, use a much shorter lifetime for it, or returning visitors may keep seeing the old styles for months.

Should I cache HTML pages in the browser?

Usually not for long. HTML holds your prices, hours, menus and new posts, and a long browser cache means visitors can see outdated content after you change it. Let your page cache plugin or your host handle HTML, since they can clear their cache when you publish. Browser caching works best for images, stylesheets, scripts and fonts.

Does Cloudflare override my .htaccess caching rules?

Sometimes. With Browser Cache TTL set to Respect Existing Headers, Cloudflare passes your headers through. With another setting, Cloudflare replaces your values when they're shorter than its setting or missing entirely. Cloudflare also uses its own rules to decide what to store at the edge, and by default it doesn't cache HTML. Set it to respect existing headers so .htaccess stays in charge.

What's the difference between Expires and Cache-Control?

Expires gives a fixed date, while Cache-Control max-age gives a number of seconds from when the file was fetched. Modern browsers and CDNs prefer max-age when both are present. Apache's mod_expires module sends both at once, so you don't have to pick. For extra settings like immutable or s-maxage, you need Cache-Control set through mod_headers.

Next step

Caching headers, image formats and script cleanup are all part of my page speed tune-up, with a Core Web Vitals report before and after. Send me your URL through the speed questionnaire and I'll tell you what I'd change first.

Want a second pair of eyes on your site?

Tell me what's going on in the site speed optimization questionnaire. I reply with a plan and a fixed quote within one business day.

Starting at $600 Start the questionnaire