Disabling Caching Using htaccess Print

  • Caching, .htaccess
  • 3

How to disable caching using .htaccess

While caching is great for overall site performance, it can be an issue when you are updating your site rapidly or testing new content. To get around this, we can add the attached code into the .htaccess file to prevent caching while you are working on the site. This will allow you to see changes live without having to clear your cache after every change.

 

What is .htaccess?

The .htaccess file passes instructions to Apache and other web servers, and often contains instructions for caching. The file is set up to read from top to bottom, allowing commands at the bottom of the file to supersede commands further up. Because of this, we can insert our piece of code at the bottom of the file, allowing us to disable caching without having to move any of our existing code. This also makes it easier to return the file to normal after you are done testing.

 

How to disable caching via .htaccess

Log into your cPanel, then navigate to the file manager. Locate the .htaccess file for the site you are working on, which should be inside the "public_html" folder(you may have to click settings in the upper right corner and turn on "Show Hidden Files"). Click the file, then click "Edit. Once inside, add the following code to the bottom of the file. When complete, hit save before exiting. When you are finished working on the site, simply remove this piece of code to return caching to your previous settings.

 

 

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

 


Was this answer helpful?

« Back