How can I prevent Web browsers from caching my pages?
When you change a page on your site, the change takes effect immediately on our servers.
However, if someone visits the same page twice in a short period of time, they might not always see the change. That happens when their Web browser "caches" it the first time, then decides there's no reason to waste time contacting the server to display the exact same page a second time. That speeds up browsing, but causes problems if the page on the server really has changed.
It's possible for technically advanced customers to change how Web browsers cache the files.
On this page:
- Setting a short cache time
- Controlling which files are affected
- Your script may already do this for you
- Try to avoid "no-cache"
- FastCGI and subdirectories
- You can't control everything
Setting a short cache time
By asking the Web browser to only cache the file for a very short length of time, you can usually avoid the problem.
ExpiresActive On ExpiresDefault A1 Header append Cache-Control must-revalidate
That's all it takes. The next time the visitor views the file, more than a second will have passed, so the browser won't use the outdated cached copy.
Controlling which files are affected
Disabling browser caching will slow down your site and increase the amount of bandwidth your site uses, because repeat visitors will always connect to your site to re-download files they would otherwise cache.
To minimize that impact, you might want to prevent caching of files that change often (such as HTML files), while allowing normal caching of files that don't (such as JPEG files).
To do this, include the .htaccess lines in a <FilesMatch> directive. For example, these lines will ask the browser not to cache “.pdf” files, without affecting the caching of other files:
<FilesMatch "\.pdf$"> ExpiresActive On ExpiresDefault A1 Header append Cache-Control must-revalidate </FilesMatch>
And these lines will prevent caching of filenames ending in “.htm” or “.html”, while allowing normal caching of other files:
<FilesMatch "\.(htm|html)$"> ExpiresActive On ExpiresDefault A1 Header append Cache-Control must-revalidate </FilesMatch>
(An alternate way of doing this is to use the Apache "
ExpiresByType text/html" command, but that doesn't allow you to add the "Cache-Control: must-revalidate" header for only those pages.)
Your script may already do this for you
If you use a script to generate your Web pages, it may already include appropriate headers to prevent page caching.
We recommend using the above commands only on static pages (like ".html" pages), or in cases where you're sure your script doesn't already do it.
Try to avoid "no-cache"
You'll sometimes see people suggest using these lines in a .htaccess file, instead of just setting the cache time to a very short number:
Header unset Cache-Control Header append Cache-Control "no-cache, must-revalidate"
This causes Microsoft Internet Explorer to avoid saving files in the "Temporary Internet Items" folder at all. That sounds like what you want — but it causes problems if Internet Explorer tries to use an external viewer to display the file.
We recommend setting the cache time to one second instead.
FastCGI and subdirectories
If you're using FastCGI for PHP scripts, some "Header" commands will be ignored from .htaccess files that are within subdirectories — only rules in the top-level .htaccess file will be used. Make sure your rules are at the top level.
You can't control everything
The techniques we suggest above almost always work. But remember that the headers you can set on the Web server are merely asking the browser to do something. They can't force the browser to do something it doesn't want to do.
A common Internet complaint is that a small handful of browsers or "proxy caches" don't cache things properly no matter what you do, due to bugs or misconfigurations. The tips above will work in almost all cases, but the nature of the Internet means you can't guarantee it will always work the way you hope.
Copyright © 2000-2019 Tiger Technologies LLC