How to disable caching using the .htaccess file
There are times when you must disable caching of your website files. You would want to stop it by letting any browser accessing your site know that it should not cache the files. The best way to do this is to use directives written in the .htaccess file. As we are using the Apache server there are a couple ways of adding these commands in the .htaccess file. It may depend on the software you are using to display your website. If you are unsure, then you should speak with a developer to be sure.
Conditions for Disabling Caching
Some conditions for not wanting caching on a viewer's internet browser include:
- Constantly updated data that you do not want to be cached (e.g. stock ticker)
- Caching structures that you prefer to use on your server
- Pages requiring login verification that include updates that change regularly
.htaccess Code for Disabling Caching
There are a few different versions of code that you can add to disable cache. We have provided two examples of code that you can use in the .htaccess file to disable caching. The older version identifies the file types that you do not want to be cached. The newer version which uses cache-control to use in .htaccess. Please feel free to look at the code and use it in your .htaccess file to disable caching if necessary.
Things to consider when adding the code to .htaccess
Remember that .htaccess files are read from top to bottom, so if you want these directives to occur before something else, then they should be placed higher within your .htaccess file. Bear in mind that there may be multiple .htaccess files and that .htaccess files placed above another in a directory will have precedence over the one placed in a sub-directory.
Examples of the code used to disable caching
<FilesMatch "\.(css|flv|gif|htm|html|ico|jpe|jpeg|jpg|js|mp3|mp4|png|pdf|swf|txt)$"> <IfModule mod_expires.c> ExpiresActive Off </IfModule> <IfModule mod_headers.c> FileETag None Header unset ETag Header unset Pragma Header unset Cache-Control Header unset Last-Modified Header set Pragma "no-cache" Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate" Header set Expires "Thu, 1 Jan 1970 00:00:00 GMT" </IfModule> </FilesMatch>
# DISABLE CACHING <IfModule mod_headers.c> Header set Cache-Control "no-cache, no-store, must-revalidate" Header set Pragma "no-cache" Header set Expires 0 </IfModule>
This completes the tutorial on disabling caching using the .htaccess files. For more information on the .htaccess file, please see Where is my .htaccess File?
You are NOT logged in. You can still browse our Support Center.
To participate within our Community Support Forum:
2018-09-25 12:10 am
What is the difference in these two methods? Is one better than the other for certain cases?
2018-09-25 8:43 am
The differences are explained in the text of the article. Both have the same effect. It depends on what you're trying to do. I'd advise trying both.