How to Disable Caching Using .htaccess


how to disable caching using .htaccess

Sometimes, you may need to disable cache altogether, if you are making updates to your website, of it is giving errors. Here’s how to disable caching using .htaccess in Apache web server.

 

How to Disable Caching Using .htaccess

Here are the steps to disable caching using .htaccess. Before you proceed, please ensure you have enabled .htaccess (mod_rewrite) in your Apache web server. Here are the steps to do it on:

  1. Ubuntu/Debian systems
  2. CentOS and similar systems

.htaccess allows you to modify Apache server config without actually accessing the config file. You will typically find it in your website’s root folder (/var/www/html).

 

There are 2 ways to disable caching using .htaccess

  1. Disable cache for specific file types
  2. Disable cache all over your website

 

Disable cache for specific file types

Add the following lines of code to your .htaccess to disable caching for specific file types such as css, jpeg, etc.


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

 

The above code will disable cache for files whose extensions match the ones you have provided in the FilesMatch block

 

Disable cache all over your website

If you want to completely disable cache on your website, add the following code to your .htaccess file.


# 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>

 

Please note:

  1. .htaccess file is processed from top to bottom. So if you want the above lines of code to be processed before some other block of code, place it above that block.
  2. If your website has multiple .htaccess files, the one placed in a directory takes precedence over the one in subdirectories.

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!