Improve website performance by optimizing caching on your Apache server. Learn how to set Cache-Control headers in your .htaccess file to reduce load times, enhance user experience, and lower server load.
Most slow sites aren’t slow because of one heavy thing. They’re slow because the browser re-downloads the same logo, stylesheet, and script on every single visit. A cache header fixes that. You tell the browser to hold onto files it already has instead of asking for them again.
On Apache you set this in your .htaccess file (root directory) using mod_headers. Here’s the version worth copying, which caches your static assets for 30 days:
<filesmatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css)$">
Header set Cache-Control "max-age=2592000, public"
</filesmatch>Two things to know. max-age=2592000 is the freshness window in seconds, 30 days here, so the browser treats the file as good to reuse for that long. public means any cache can store it, including a CDN.
One honest caveat, and it’s the part most copy-paste snippets get wrong. Don’t put a 30-day max-age on your HTML. HTML is where your content actually changes, so cache it that long and visitors keep seeing yesterday’s page. Give it a short window instead, or zero, and let the assets carry the heavy caching:
<filesmatch ".(html|htm)$">
Header set Cache-Control "max-age=3600, public"
</filesmatch>Long caching on static files is only safe if you change the filename when the file changes. Ship style.css as style.4f2a.css, or add style.css?v=2. New name, new download, no stale cache. Call it versioning or cache busting, same idea: it lets you cache aggressively without trapping users on old files.


