How to set browser caching headers
How to tell a browser what it can keep and for how long, so a repeat visit does not re-download everything.
The fastest request is the one a browser never makes. A first-time visitor has to download everything on a page — every image, stylesheet and script — but a returning visitor does not have to, provided the browser has been told it is allowed to keep what it downloaded last time and reuse it. That instruction is what browser caching headers are for.
Without them, a browser has to guess how long it can safely reuse a file, and browsers guess conservatively — which means a repeat visitor ends up re-downloading files that had not actually changed since their last visit.
The two headers that matter
| Header | What it does | Notes |
|---|---|---|
| Cache-Control | Sets how long a file can be reused, in seconds, and under what conditions | The modern standard. Takes precedence over Expires when both are present. |
| Expires | Sets a fixed calendar date after which the file must be re-fetched | Older, still widely supported, useful as a fallback alongside Cache-Control. |
A typical Cache-Control header looks like this:
Cache-Control: max-age=31536000, public
max-age is the number of seconds the browser may reuse the file without checking back with the server at all. public confirms the file can be cached by shared caches too, such as a CDN, and not only by the visitor's own browser.
How long to cache different file types
Not everything should be cached for the same length of time, because not everything changes at the same rate. Static assets that rarely or never change once published can be cached for a long time; HTML that might be updated at any moment should not be.
| File type | Typical approach |
|---|---|
| Images, fonts, videos | Long cache duration — these rarely change once uploaded |
| CSS and JavaScript | Long cache duration, combined with a versioned filename so an update is served under a new name rather than waiting for the cache to expire |
| HTML pages | Short duration or no long-term caching in the browser — content can change at any time |
Caching a CSS file for a long period is only safe if you can force a fresh download the moment it changes. The standard way to do this is to include a version string or hash in the filename — style.css?v=3 or style.a1b2c3.css — so an update ships under a new URL that has never been cached, while the old file can safely expire in the background.
Setting it with .htaccess
On Apache-based hosting, both headers are typically set through mod_expires and mod_headers in your site's .htaccess file:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/webp "access plus 1 year"
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType text/html "access plus 0 seconds"
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "\.(css|js|webp|jpg|png)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>
</IfModule>
The <IfModule> wrapper means the block is skipped safely if a module is not present rather than causing an error. See how to use .htaccess for general rules on editing this file without breaking the site.
A mistake here can produce a server error across the whole site, not just fail to apply the caching rule. Keep a copy of the current file so you can put it straight back if something goes wrong.
How to check it is working
Open your browser's developer tools, go to the network panel, and reload the page twice. On the second load, files served from the browser's own cache typically show as coming "from disk cache" or "from memory cache" rather than showing a network transfer size, and clicking into an individual request will show the Cache-Control header in its response.
-
Load the page once normally
This populates the cache for the first time.
-
Reload the page and check the network panel
Cached files should show a cache indicator instead of a transfer size.
-
Inspect the response headers on a cached file
Confirm
Cache-Controlshows the duration you actually set. -
Test as a private or incognito window too
This rules out anything left over from earlier testing giving a false result.
What this does and does not fix
Browser caching helps returning visitors, not first-time ones — a first visit still has to download everything at least once, so this is not a fix for a slow first impression. It is complementary to, and different from, page caching, which speeds up how a page is generated on the server, and to a CDN, which shortens the network distance a file has to travel. All three address a different part of the same overall picture, and a well-optimised site typically uses all three together rather than treating any one of them as sufficient on its own.
Related reading
How to turn on text compression for your site, and how to check it is actually being applied afterwards.
How page caching worksWhy rebuilding a page from scratch on every visit is expensive, and what caching actually does instead.
How to use .htaccess on your hosting accountOne file that controls redirects, rewrites and access rules for a whole folder — and one typo in it that can take the site offline.
How to clear your browser cache properlySupport will often ask for this first, because a stale cached copy of a page is a surprisingly common cause of a fault that has already been fixed.