Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this FilesMatch not matching correctly?

Tags:

apache

tomcat

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?

like image 275
David Shaw Avatar asked May 23 '13 16:05

David Shaw


1 Answers

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.

like image 96
John Mark Mitchell Avatar answered Oct 12 '22 23:10

John Mark Mitchell