Caching enhances the performance of a website. Various instructions in the .htaccess file set headers that instruct browsers to retain files for an extended period. When a site is being modified it is useful to turn caching off so changes are seen immediately. This article provides the necessary code and instructions for turning off caching using .htaccess.
The .htaccess file provides a way to give instructions to the Apache and compatible web servers. Caching instructions are often placed in .htaccess. One interesting property of .htaccess is that it reads intructions from top to bottom. If an instruction at the top is contradicted by one farther down, the last one to be read will be the one that is used. This allows us to turn off caching without disurbing any existing instructions simply by putting new instructions at the bottom. When site updates are finished, our additional instructions can be deleted to return the site to normal operation.
Add this code at the bottom of the .htaccess file to turn off caching. Delete the code to turn caching on again.
# 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> <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>