How to turn off caching using .htaccess

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

<IfModule mod_headers.c>
    Header set Cache-Control "no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires 0

<FilesMatch "\.(css|flv|gif|htm|html|ico|jpe|jpeg|jpg|js|mp3|mp4|png|pdf|swf|txt)$">
    <IfModule mod_expires.c>
        ExpiresActive Off
    <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"
Last updated byChris Grant (he/him)Chris Grant (he/him) on 2nd March 2020