Two things in an .htaccess file genuinely speed up a website: compressing text files and letting the browser keep files in its cache for longer. Both are a dozen lines to copy. But before you copy anything, it helps to know which rule helps people, which only bumps your PageSpeed score - and which can do damage.
In short
- Compression (gzip) makes CSS, JS and HTML files several to a dozen times smaller. In my test, a CSS file went from 23,490 to 1,094 bytes.
- A long cache for CSS, JS, images and fonts saves downloading them again on every page.
- Do not cache HTML pages or PHP output, or visitors will see an old version.
First, check what you already have
Many hosts set compression and caching at server level. Before adding rules, open your browser's developer tools (F12), the Network tab, click a CSS file and look at the response headers. You are after two:
Content-Encoding: gzip(orbr) - compression works,Cache-Control: max-age=...- the browser knows how long to keep the file.
If both are there, treat this article as background reading 🙂
Compression
Text files compress very well because they are full of repetition. The server packs a file before sending it, and the browser unpacks it in a fraction of a second.
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript application/javascript application/json image/svg+xml
</IfModule>
I measured it on a local Apache 2.4.68 server: a stylesheet of 23,490 bytes arrived as 1,094 bytes. That is an extreme case, as the file was very repetitive, but ordinary CSS or JavaScript still shrinks several times.
What not to compress: JPG, PNG and WebP images, videos, ZIP and PDF files. They are compressed already, and a second pass only costs the server CPU time. That is why the list above names text types rather than "everything". Images get smaller another way: by compressing them and converting to WebP before they go on the server.
Browser cache
Without cache headers, on every page the browser asks the server whether the logo, the CSS and the fonts have changed. With them, it takes them from disk and does not ask at all.
<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 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType font/woff2 "access plus 1 year"
</IfModule>
A year sounds bold. What if you change the CSS? The browser will keep the old version... unless the file gets a new address. That is why WordPress and most systems add a version: style.css?ver=1.5.0. A new version is a new address, so a fresh download. If your site does not do this, use a shorter time, a week for example.
The trap: ExpiresDefault
Many guides include this line:
ExpiresDefault "access plus 1 month"
It looks harmless: "everything else for a month". Except that "everything else" includes pages generated by PHP. In my test a PHP script's response got Cache-Control: max-age=3600, the hour from ExpiresDefault. I had set zero for text/html, but the script happened to return plain text (text/plain), so that rule did not cover it. In practice, visitors see an old basket, an old price list, or do not see that they have logged out. So name the file types explicitly and leave ExpiresDefault alone.
Cache-Control for chosen files
Alternatively (or as well), you can set the header by file extension:
<IfModule mod_headers.c>
<FilesMatch "\.(css|js|webp|jpe?g|png|svg|woff2)$">
Header set Cache-Control "public, max-age=31536000, immutable"
</FilesMatch>
</IfModule>
immutable tells the browser: this file at this address will never change, do not even check it when the page is reloaded. It only makes sense when your files carry a version in their address.
What only improves a tool's score
Tools such as PageSpeed Insights like to point out things that make little difference to people:
- "Serve static assets with an efficient cache policy" for scripts from other domains (analytics, ads, external fonts) - you have no control over them, your .htaccess does not cover them.
- Compressing very small files - at a few hundred bytes the gain is negligible.
- Settings your host already has - adding them a second time changes nothing.
The biggest wins are usually things .htaccess does not hold: lighter images, fewer plugins and scripts, decent hosting. Compression and caching are a quick win, but they do not replace a clean-up.
The ready set
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript application/javascript application/json image/svg+xml
</IfModule>
<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 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType font/woff2 "access plus 1 year"
</IfModule>
The <IfModule> blocks mean that if a module is missing on the server, Apache simply skips the rules instead of returning a 500 error. After uploading, check the headers in the Network tab again, and if something does not work, see Debugging .htaccess. If your site sits behind a CDN, remember it has a cache and settings of its own.