Add Expires Headers in .htaccess (and Why the Warning Might Not Go Away)
Every time someone opens a page, their browser can ask your server for the same logo, stylesheet and fonts all over again. Expires headers tell the browser it may keep those files for a set time, so the second page a visitor opens, and every later visit, loads much faster.
Speed tests such as GTmetrix show "Add Expires headers" when your files are sent without this instruction. This guide covers Apache and LiteSpeed servers, which run most shared and cPanel hosting.
Expires and Cache-Control
There are two headers that do this job:
Expiresgives a date and time until which the file can be reused.Cache-Control: max-agegives the same thing as a number of seconds.
Modern browsers follow Cache-Control when both are present. The rules below make Apache send both, which satisfies older and newer speed tests alike.
Step 1: Check what your server sends now
- Open your website and press F12 to open the developer tools.
- Open the Network tab and reload the page.
- Click a CSS file or an image in the list and look under Response Headers.
- Look for
expiresandcache-control.
If they are missing, or cache-control says max-age=0 or no-cache, that file is not being cached.
Step 2: Add the rules
Open the .htaccess file in your website's main folder (usually public_html) and download a copy as a backup. Then add:
<IfModule mod_expires.c>
ExpiresActive On
# Pages: always check for a fresh copy
ExpiresByType text/html "access plus 0 seconds"
# Stylesheets and scripts
ExpiresByType text/css "access plus 1 year"
ExpiresByType text/javascript "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
# Images
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresByType image/vnd.microsoft.icon "access plus 1 year"
# Fonts
ExpiresByType font/woff2 "access plus 1 year"
ExpiresByType font/woff "access plus 1 year"
ExpiresByType application/font-woff "access plus 1 year"
</IfModule>
Pages are set to zero on purpose. Your pages change, for example when you update prices or text, and visitors should always get the latest version. Files like images, styles and fonts rarely change, so a long cache time is safe, as long as you deal with the catch in Step 3.
Step 3: Stop visitors seeing your old CSS
With a one-year cache, a returning visitor's browser keeps using your old stylesheet even after you upload a new one, so your changes seem not to appear.
The fix is to change the file's address whenever the file changes, by adding a version number:
<link rel="stylesheet" href="/assets/style.css?v=2">
Change v=2 to v=3 each time you edit the file. On a PHP site you can let the server do it automatically using the file's last-modified time:
<link rel="stylesheet" href="/assets/style.css?v=<?= filemtime(__DIR__ . '/assets/style.css') ?>">
WordPress already adds a ?ver= number to most stylesheets and scripts that themes and plugins load.
Step 4: Test again
Repeat Step 1 on a CSS file and an image. You should now see an expires date about a year ahead and cache-control: max-age=31536000. Then run a fresh speed test.
Why the warning can stay
- mod_expires is not enabled. The
IfModulewrapper stops errors but means nothing happens if the module is missing. Ask your host to enable it. - The file type does not match. Rules work by file type, so if your server sends a font as
application/octet-stream, the font rule never applies. Check thecontent-typeheader in Step 1 and add a rule for that exact type. - The files are not yours. Analytics, ads, chat widgets and fonts loaded from other websites follow their owners' cache settings, which you cannot change.
- Cloudflare is overriding your headers. In Cloudflare's caching settings, set Browser Cache TTL to "Respect Existing Headers" so your own rules are used.
- A caching plugin writes its own rules. Two sets of caching rules can conflict. Use one.
Need it done for you?
If the warning will not clear, or changes to .htaccess broke something, we can sort it out as part of our PHP and website fix service.