There are times when you must disable caching of your website files. To achieve this, you need a way to force any web browser accessing your site to not cache one or more types of files. For Apache web servers, the best solution is to edit .htaccess to disable caching.
Common reasons 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 server-side (e.g. NGINX or Cloudflare proxy servers)
- Pages requiring login verification that include frequent updates
There may be easier methods depending on your workflow and software. If you are unsure, contact the software developer(s) for further assistance.
Before You Edit .htaccess to Disable Caching
There are a few different versions of code that you can add to disable website caching . We have provided two examples of code that you can use in the .htaccess file to disable caching. The first example identifies the file types that you do not want to be cached. The second code block uses cache-control Apache header.
A few things to consider when adding the code to .htaccess:
Remember that .htaccess files are read from top to bottom. 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 in parent directories which have precedence over the one placed in a sub-directory.
Example 1, great for caching websites while excluding certain file types which are updated often:
<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>
Example 2, ideal for other sites:
# 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 modifying .htaccess to disable caching. For more information on the .htaccess file, please see Where is my .htaccess File?