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

As a writer for InMotion Hosting, Arnel has always aimed to share helpful information and provide knowledge that will help solve problems and aid in achieving goals. He's also been active with WordPress local community groups and events since 2004.

More Articles by Arnel

4 Comments

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

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

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

Leave a Reply to InMotionFan Cancel reply