Apache Web Server allows you to cache content to speed up your website and reduce server load. However, sometimes, you might need to disable Apache Cache if it is not performing as per your requirements. Let us look at how to Disable Apache Cache
Here are some reasons why you may want to disable Apache cache:
- Your data is constantly updated so you don’t want it to be cached
- Pages require user login and update regularly
How to Disable Apache Cache
The easiest way to disable Apache cache is to add directives in .htaccess file. For this, you need to enable .htaccess in Apache web server. If you haven’t enabled .htaccess on your Apache server, here’s how to do it:
.htaccess allows you to modify Apache Server settings without accessing its config files. You will find it in your website’s root folder (/var/www/html). If you don’t find it, just create a new file named .htaccess.
There are 2 ways to disable Apache cache using .htaccess. The first one disables cache for specific file types only. The second disables cache overall.
<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>
# 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>
- .htaccess files are processed from top to bottom. So if you want these directives to be processed before some other block of code, you need to place them above that piece of code.
- In case your website has multiple .htaccess files, the one placed in a directory takes precedence over the ones placed in subdirectories.