How to set browser caching via .htaccess
Browser caching tells visitors to keep your images, CSS, and scripts locally so they are not re-downloaded on the next visit. It is the simplest way to speed up repeat views, and all you need is access to .htaccess.
Check your site
-
Open .htaccess in your site root
In cPanel open File Manager, turn on Show Hidden Files under Settings, and open .htaccess inside public_html. Save a copy of the file before editing it.
-
Set cache lifetimes with mod_expires
Add an <IfModule mod_expires.c> block with ExpiresActive On and ExpiresByType rules, for example images and fonts for 1 year, CSS and JS for a month.
-
Mirror the rules with Cache-Control
Inside <IfModule mod_headers.c> add Header set Cache-Control "max-age=31536000, public" for static files. Cache-Control is more modern than Expires, but together they cover every browser.
-
Separate static files from HTML
Apply long caching only to files that rarely change: img, css, js, woff2. Keep HTML pages on a short cache or no-cache, or visitors will be stuck on an old version.
-
Version files that change
So an updated CSS is picked up despite long caching, change its name or query string: style.css?v=2. The browser then treats it as a new file and downloads it again.
-
Save and check the site
Save .htaccess and open the site. If you get a 500 error, remove the last block you added, since the server may not have mod_expires.
How to verify the result
Run the TechGuard speed test and look at the headers of your static files: they should now show Cache-Control with max-age and an Expires date in the future. That confirms caching is set.
Tip: Do not put a long cache on HTML pages, or visitors will stay stuck on an old version and miss your updates.
Related guides
Found problems?
Slow hosting, SSL trouble or frequent downtime? Move to TomisHost: fast hosting with free SSL, daily backups and free migration help.
TomisHost hosting