A simple Guide to Disabling Caching with .htaccess

Caching is a crucial technique for improving website performance and reducing load times. However, there are scenarios where you might want to disable caching for specific files or content to ensure users receive the most up-to-date information. In this tutorial, we’ll explore how to achieve this using the .htaccess file on your web server.

What is Caching and Why Disable It?

Caching involves storing copies of frequently accessed files or resources either on the client’s side (browser or device) or on the server-side. While caching can significantly boost load times, there are times when you need to disable it to ensure that users always receive the latest content. This might be necessary for rapidly changing content, such as news articles, dynamic pages, or frequently updated data.

The .htaccess Approach

The .htaccess file is a configuration file that can be placed in your website’s root directory to control various aspects of how your web server behaves. To disable caching, we’ll use a combination of HTTP header manipulations. Here’s how you can achieve this:

1. Disabling Caching Headers

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

In this section, we’re using the mod_headers module to set specific headers that instruct browsers and clients not to cache the content. Let’s break down each line:

  • Cache-Control: This header controls how caching should be performed. "no-cache, no-store, must-revalidate" directs the browser to revalidate content with the server before displaying it, effectively disabling caching.
  • Pragma: Older browsers might not understand the Cache-Control header, so the Pragma header provides a fallback mechanism for preventing caching.
  • Expires: Setting the expiration date to a past time (0 here) ensures that the content is considered expired immediately.

2. Disabling Caching for Specific File Types

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

This section targets specific file types using a regular expression pattern. Here’s what’s happening:

  • <FilesMatch>: This directive allows you to specify a regular expression pattern to match certain file types.
  • mod_expires: If available, it’s used to deactivate the Expires header for the specified file types, ensuring they are not cached.
  • mod_headers: Similar to the previous section, we’re unsetting various caching-related headers and then setting headers that prevent caching.

Implementing the Code

  1. Create or edit the .htaccess file in your website’s root directory.
  2. Paste the provided code snippet into the .htaccess file.
  3. Save the file and upload it to your web server.

Additional Resources

  1. Apache HTTP Server Documentation: Official documentation for Apache HTTP Server, including .htaccess configuration details.
  2. Caching Tutorial for Web Authors and Webmasters: A comprehensive guide to understanding caching and its implications.
  3. HTTP Caching: Mozilla Developer Network’s guide to HTTP caching mechanisms and best practices.


Disabling caching for specific files or content ensures that your users receive the latest and most accurate information from your website. By leveraging the power of the .htaccess file and manipulating HTTP headers, you can control caching behavior and enhance the user experience. Just remember that while disabling caching has its benefits, judicious use is recommended to strike a balance between performance and freshness of content.