Economicalhost · Knowledge Base
Knowledge Base › PHP & Performance › How to Leverage Browser Caching with .htaccess
PHP & Performance

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

  1. Open public_html/.htaccess in File Manager (show hidden files first) and back up its current content.
  2. 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>
  3. Save the file.
  4. Test with your browser dev tools — static files should return a cache/expires header on reload.
  5. Remember to bump versions when you change a cached CSS/JS file (e.g. style.css?v=2) so visitors get the update.
  6. 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.