14

We have been attempting to configure our server not to cache our .htm files as it is causing a few issues with our analytics package as well as not displaying the pages correctly if the visitor hits the back button in their browser.

We have attempted to tackle it by adding:

<FilesMatch "\.(htm)$">
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"
Header set Warning "Testing"
</FilesMatch>

to our httd file but it does not appear to execute, however, when we move the Header set outside of the FilesMatch it appears to execute fine..

Anyone have any ideas where we are going wrong?

MrWhite
  • 43,179
  • 8
  • 60
  • 84
David Shaw
  • 565
  • 1
  • 4
  • 12

2 Answers2

20

I recently needed to figure out the same kind of problem and, although this post pointed me in the right direction, I wanted to share some clarifying information for the edification of those who search on this topic in the future.

David, your initial FilesMatch was not working because FilesMatch only works on real, physical files that exist on your filesystem. http://httpd.apache.org/docs/current/sections.html states it as:

The Directory and Files directives, along with their regex counterparts, apply directives to parts of the filesystem.

This is also why your second post using LocationMatch resolved the issue. Also from http://httpd.apache.org/docs/current/sections.html, it states:

The Location directive and its regex counterpart, on the other hand, change the configuration for content in the webspace. < SNIP > The directive need not have anything to do with the filesystem. For example, the following example shows how to map a particular URL to an internal Apache HTTP Server handler provided by mod_status. No file called server-status needs to exist in the filesystem.

<Location /server-status>
    SetHandler server-status
</Location>

The Apache docs summarizes this behavior with the following statement:

Use Location to apply directives to content that lives outside the filesystem. For content that lives in the filesystem, use Directory and Files. An exception is < Location / >, which is an easy way to apply a configuration to the entire server.


For those that want to understand more of the mechanics, this is how I understand the internals:

  • Location directives match based on the HTTP request URI (e.g. example.com/this/is/a/uri.htm without the example.com part).
  • Directory and Files directives, on the other hand, match based on whether there is a directory path or file in the filesystem of the DocumentRoot that matches to respective part of the the HTTP request URI

The Apache docs summarizes this behavior as:

What to use When

Choosing between filesystem containers and webspace containers is actually quite easy. When applying directives to objects that reside in the filesystem always use Directory or Files. When applying directives to objects that do not reside in the filesystem (such as a webpage generated from a database), use Location.

[IMPORTANT!] It is important to never use Location when trying to restrict access to objects in the filesystem. This is because many different webspace locations (URLs) could map to the same filesystem location, allowing your restrictions to be circumvented.

Community
  • 1
  • 1
John Mark Mitchell
  • 4,522
  • 4
  • 28
  • 29
  • 1
    Thanks a LOT John and also David. I loose a lot of time with this and a "detail": if you have an Apache Webserver and use a "virtual server" on it, when you edit etc/apache2/sites-available/your_server.conf trying using Files or FilesMatch nothing will happened. You have to use Location. – Peter Mar 08 '15 at 18:41
12

This issue has now been resolved.

In order to get it to work we have changed from using FilesMatch to LocationMatch and now the headers are being set perfectly.

We believe this is because the page is being redirected from a JSP page to an HTML page.

<LocationMatch "\.(htm|html)$">
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"
Header set Warning "Testing"
</LocationMatch>

Hopefully others will find this helpful.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
David Shaw
  • 565
  • 1
  • 4
  • 12