Force no caching in Apache by modifying the .htaccess file using appropriate headers

CK CK 3788 
Created at  
2,674 0 0

You can force no caching in Apache by modifying the .htaccess file using appropriate headers. Here are a few methods:

1. Disable Caching Using Headers

Add the following to your .htaccess file to prevent browsers and proxies from caching your files:

<IfModule mod_expires.c>
    ExpiresActive Off
</IfModule>

<IfModule mod_headers.c>
    Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
    Header set Pragma "no-cache"
    Header set Expires "Thu, 01 Jan 1970 00:00:00 GMT"
</IfModule>

Explanation:

  • Cache-Control: no-store, no-cache, must-revalidate, max-age=0 → Prevents browsers from caching.
  • Pragma: no-cache → For older HTTP/1.0 caches.
  • Expires: Thu, 01 Jan 1970 00:00:00 GMT → Ensures files are considered expired immediately.

2. Disable Caching for Specific File Types

If you only want to disable caching for certain file types (e.g., .html, .css, .js), use:

<FilesMatch "\.(html|css|js|png|jpg|gif)$">
    Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
    Header set Pragma "no-cache"
    Header set Expires "Thu, 01 Jan 1970 00:00:00 GMT"
</FilesMatch>

3. Disable Caching for a Specific Folder

To prevent caching for a specific directory (e.g., /nocache), create an .htaccess file inside /nocache/ with:

Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
Header set Pragma "no-cache"
Header set Expires "Thu, 01 Jan 1970 00:00:00 GMT"

4. Force File Changes Using Query Strings (Alternative Method)

If caching issues persist, another approach is to append query strings when linking assets:

<link rel="stylesheet" href="style.css?v=<?php echo time(); ?>">
<script src="script.js?v=<?php echo time(); ?>"></script>

This forces browsers to always fetch the latest version.


5. Final Step: Restart Apache (if needed)

If changes don’t apply immediately, restart Apache:

sudo systemctl restart apache2  # For Debian/Ubuntu
sudo systemctl restart httpd     # For CentOS/RHEL

 

Tags Apache Cache Force No Caching No Caching no-cache no-store Facebook X
Comments 0
  1. Why SPF and DKIM Are Essential for Linux Email Servers?
    34
  2. Upgrading to GCC 4.9.4 in CentOS 7 requires installing a newer version manually
    583
  3. Beyond your conventional memory size with swap file instead of a partition
    1,142
  4. Gmail requires all senders to authenticate with either SPF or DKIM.
    681
  5. Bash script to install postfix and sasl for sending email
    1,195
  6. Guide for tmux shortcuts - split current pane, toggle pane
    1,300
  7. Tistory remote blogging example based in CLI & PHP
    2,719
  8. Generate Let’s Encrypt Wildcard SSL Certificate
    626
  9. Preventing Unauthorized Image Crawling by Bots: A Clever Discovery and How to Address It
    1,727
  10. Network diagnostic tool for IPv4/IPv6 - traceroute/traceroute6
    1,918
  11. chmod by type (directory and file permission)
    734
  12. Check network port is open or not on CentOS 7
    1,188
  13. Change MySQL port number for security purpose
    815
  14. CentOS warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory on CentOS 7
    1,072
  15. Set up an SMTP server on CentOS 7
    726
  16. How to mount windows shared folder on Centos 6.8 - mount error 12 = Cannot allocate memory to the mount.cifs(8)
    912
  17. Configuring IP Address on Centos 6.7
    789
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    40
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    34
  3. How do I create animated background for Google Meet?
    20,611
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    244
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    70
  11. Frequency Cap Architecture Overview
    514
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    287
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    230
  16. TPU & GPU Cluster Management Tools
    244
  17. The Concept of Scaffolding in AI Agents
    233
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    258
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    221
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    220
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    195
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    217
  30. Understanding Data Lineage
    260