2 min readApache, Nginx, Tools 🛠️

How to Turn Off Website Caching: Step by Step

Hi, web developers! Today we’ll show you how to turn off website caching. This is important, especially when you’re testing new changes and want to see them right away.

1. Turning off caching on the server

Apache:

  1. Open the .htaccess file.
  2. Add the following code:
Apache
<FilesMatch "\.(html|htm|css|js)">
    FileETag None
    <IfModule mod_headers.c>
        Header unset ETag
        Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
        Header set Pragma "no-cache"
        Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
    </IfModule>
</FilesMatch>

Nginx:

  1. Open the site’s configuration file (for example, nginx.conf).
  2. Add the following code to the matching server block:
Nginx
location ~* \.(html|htm|css|js)$ {
    expires -1;
    add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0";
    add_header Pragma "no-cache";
}

2. Turning off caching in HTML

To turn off caching for HTML pages, add the following meta tags to your HTML file:

HTML / XML
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">

3. Turning off caching in PHP

Add the following code to your PHP file:

PHP
<?php
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");
?>

Turning off caching lets you test new changes on your site with confidence, making sure the content you see is up to date. Remember that caching can be useful for site speed, so you can restore its settings once you’ve finished testing.

I hope this guide was useful! Leave your questions in the comments and share your own life hacks. See you!

#webdevelopment #webdev #coding #developers #cache #webperformance #programming #codinglife #webdesign #tech #webdevtips #zimin_dev #zimin_dev_blog

Sasha Zimin

Leave a Reply

Your email address will not be published. Required fields are marked *