Skip to content

Setting browser cache headers at vhost or .htaccess on cPanel

Set browser cache headers on a cPanel server so returning visitors load your CSS, fonts and images from their own browser instead of downloading them again. It covers the .htaccess and vhost methods, and how to keep a deploy from serving stale files.

Browser cache headers are one of the cheapest speed fixes for returning visitors on a WordPress or PHP site hosted on cPanel. These headers, Cache-Control and Expires, tell the visitor’s browser how long it may keep its own copy of a file such as your logo, web font or main image. Without them, every return visit asks the server whether each file has changed and waits for a 304 (“not modified”) reply before the page can finish. With them, the browser uses its copy and your server never sees the request. You can set them in an account’s .htaccess or in an Apache vhost include, and the only real cost is planning how new versions reach visitors after a deploy.

What’s happening

Cache-Control: public, max-age=31536000, immutable tells the browser to cache the asset for a year and never revalidate. Expires is the older HTTP/1.0 equivalent. cPanel + Apache supports both through mod_expires and mod_headers, configured either in a per-account .htaccess or in a global vhost include. The right answer for a static URL with a hashed filename (/assets/app.a1b2c3.css) is a year. The right answer for an HTML page is short or zero — you want changes visible immediately.

How to verify

From outside the cPanel host, inspect the headers on a static asset and on the HTML page:

curl -I https://example.com/wp-content/themes/site/style.css
curl -I https://example.com/

For the CSS, you want Cache-Control: max-age=31536000 (or similar) and an Expires header a year in the future. For the HTML, you want either no Cache-Control or a short one like max-age=300. If both return identical headers, the site is either over-caching HTML (stale content) or under-caching assets (slow returning visits).

The fix

In the account’s .htaccess, between # BEGIN/END WordPress if it is WordPress:

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType text/css "access plus 1 year"
  ExpiresByType application/javascript "access plus 1 year"
  ExpiresByType image/webp "access plus 6 months"
  ExpiresByType image/jpeg "access plus 6 months"
  ExpiresByType image/png "access plus 6 months"
  ExpiresByType font/woff2 "access plus 1 year"
  ExpiresByType text/html "access plus 0 seconds"
</IfModule>
<IfModule mod_headers.c>
  <FilesMatch "\.(css|js|woff2)$">
    Header set Cache-Control "public, max-age=31536000, immutable"
  </FilesMatch>
</IfModule>

For a fleet of accounts, set this in /etc/apache2/conf.d/userdata/std/2_4/<user>/<domain>/cache-headers.conf and rebuild with /scripts/rebuildhttpdconf then /scripts/restartsrv_apache. This survives upgrades; .htaccess edits sometimes do not.

The deploy story matters: if your build does not hash filenames, you cannot use a one-year cache. Either configure your bundler to emit hashed names or accept a shorter TTL (a week, maybe a month) and live with returning visitors fetching stale-but-valid assets. Stack Harbor configures cache headers as part of cPanel/WHM management.