Preventing caching via .htaccess file (Apache)?

To completely prevent caching (storing it and using it), usually on the internet they are saying you have to use something like this in your .htaccess file:

<ifModule mod_headers.c>
     Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
     Header set Pragma "no-cache"
     Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>

Open in new window


I see this everywhere, but I don't understand it. What's the difference between the code above and this:

<ifModule mod_headers.c>
     Header set Cache-Control "no-cache, no-store"
     Header set Pragma "no-cache"
</ifModule>

Open in new window


In my opinion you could use the last one. There is already "no-cache", so there will be anyway (re)validation. So "max-age=0", "must-revalidate", and "Expires" makes no sense to me?

So for what reason exactly people are adding those things to the .htaccess file?
Maarten BruinsAsked:
Who is Participating?
 
Dr. KlahnPrincipal Software EngineerCommented:
According to this discussion, you are correct.

https://stackoverflow.com/questions/18148884/difference-between-no-cache-and-must-revalidate

With Jeffrey Fox's interpretation about no-cache, i've tested under chrome 52.0.2743.116 m, the result shows that no-cache has the same behavior as must-revalidate, they all will NOT use local cache when server is unreachable, and, they all will use cache while tap browser's Back/Forward button when server is unreachable. As above, i think max-age=0, must-revalidate is identical to no-cache, at least in implementation.

So for what reason exactly people are adding those things to the .htaccess file?

At a guess, somebody saw the longer code snippet on a web site and propagated it somewhere else.  And two becomes four, and four becomes eight, and eight becomes sixteen, and soon it is everywhere and is considered the standard, though possibly not completely correct or efficient.
1
 
Maarten BruinsAuthor Commented:
Ah thanks! I already thought, maybe there is some reason I don't know about. I also did tests with the Back/Forward button in Firefox and Chrome and I did not see any difference. And theoretically anyway it does not make sense.

Kind of weird to see how people are just copy / pasting (especially in tutorials) without thinking. But nothing to do about.

Thanks anyway for the explanation and quick answer!
0
 
Maarten BruinsAuthor Commented:
I can not leave the question open? I think this is the right answer, but if someone wants to add something more then it's not possible anymore, because the topic is closed? Looks kind of weird to me ...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.