How to Disable Caching in the .htaccess File

How to Edit .htaccess to Disable Caching

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?

AC
Arnel Custodio Content Writer I

More Articles by Arnel

6 thoughts on “How to Disable Caching in the .htaccess File

  1. hi
    my all php files have these headers :
    cache-control: no-store, no-cache, must-revalidate
    i tried to make one of my php files cache-able but i ended with this :

    cache-control: no-store, no-cache, must-revalidate
    cache-control: max-age=100, private

    why i’m having two cache-control ?!

    the code i used :


    Header unset Cache-Control
    Header set Cache-Control “max-age=100, private”

    thanks.

    1. If your .htaccess file contains anything that you believe should not be there, you may want to check with any developers or administrators who have worked on your site.

    1. 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.

      1. Hello Ausnh – “Dynamic caching” appears to be a feature of a different hosting service. InMotion Hosting provides different caching solutions. We would need more information about your hosting subscription in order to provide accurate information on your caching options. Caching can store both static and dynamic content. In the case of a 3rd party host, there is a branded system called Dynamic Caching. This service is a label of a caching feature used by website owners to help speed up the display of files from the server. You can call our live technical support team to find out more about your account’s caching options.

Was this article helpful? Join the conversation!

Questions about our MailChannels Deployment? We have answers and are here to help!Learn More
+