How to Leverage Browser Caching with .htaccess
Browser caching tells visitors' browsers to store images, CSS and JavaScript locally, so repeat visits are dramatically faster. A short .htaccess block does it.
Step-by-step
- Open public_html/.htaccess in File Manager (show hidden files first) and back up its current content.
- Add an expires block using mod_expires, for example:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
</IfModule> - Save the file.
- Test with your browser dev tools — static files should return a cache/expires header on reload.
- Remember to bump versions when you change a cached CSS/JS file (e.g. style.css?v=2) so visitors get the update.
- Pair with Gzip for compounding gains — see Gzip compression.
💡 Good to know
- Long cache times suit images; use shorter times for files you change often.
- A caching plugin or Cloudflare can manage all of this for you.
- Keep an .htaccess backup — a typo here triggers a 500 error.