Disabling Caching Using htaccess

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>

 

  • Domain, Dedicated Server, VPS Hosting, affordable hosting, Hosting India, Hosting, Reliable Web Hosting, Reseller Hosting, 99rupees hosting
  • 0 Utenti hanno trovato utile questa risposta
Hai trovato utile questa risposta?

Articoli Correlati

403 Error in the WordPress Administration [wp-admin]

What is a 403 Error? A 403 Forbidden error is returned by the server when the user has been...

BackupBuddy not working, how can I fix this?

BackupBuddy schedules things to run in the background in a way that isn't inherently compatible...

Cannot upgrade WordPress or a WordPress Plugin

Should you receive an error message about not being able to write or open a file in "/tmp"...

Drupal CPU usage spikes during backups

If you are using Drupal on your hosting accounts, you may notice sudden CPU usage spikes that...

How to run a Traceroute

What is a Traceroute and why would you need to do one? When investigating speed issues with your...