How to Set Up Non-cached Pages with .htaccess

Cache control is one of the primary system administrative tasks you will find yourself performing. Thankfully, most browsers will take care of at least some of the caching for you, remembering certain resources that were loaded before. However, there may be occasions in which browser caching is something you don’t want. In this article, we’ll show you how to set up non-cache pages with .htaccess.

For example, if you’re developing a site in a test environment and making constant changes, cached resources will likely interfere with changes you’re making. You may find yourself constantly clearing your browser cache to see the effect of your work. Or, imagine you have a static page that is updated frequently, like a client roster, or an organizational document. In cases like the latter, it would not be convenient for your visitors to constantly clear their browser caches or risk missing out on new information.

Set up non-cached pages with .htaccess

  1. Using your favorite file management method, open your .htaccess file for editing
  2. Option tip: using SSH, you can use an editor command like nano .htaccess to use the nano text editor
  3. Place this code into your .htaccess file:
     <filesMatch "\.(html|htm|js|css)$"> FileETag None   
          <ifModule mod_headers.c>      
             Header unset ETag      
             Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"      
             Header set Pragma "no-cache"      
             Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"   
          </ifModule> 
    </filesMatch>

Remember that this .htaccess file will act recursively upon all files within the document path that follows. So it would be best to separate this path from any resources you want to cache. For example, a subdomain would be a great place to put this code in order to set up non-cache pages with .htaccess.

Leave a Reply