As has already been mentioned, the <Location>
directive is not permitted in .htaccess
files. If /foo/bar/
relates directly to a filesystem directory then you can place the .htaccess
file in that directory to apply the directives (Header
in this case) to that directory and below only.
However, if /foo/bar/
is a URL-path only (which is also what the <Location>
directive targets in a server context), which does not relate directly to the filesystem, then you can instead set an environment variable when that URl-path is accessed and set the Header
conditionally based on whether that env var is set.
For example:
SetEnvIf Request_URI "^/foo/bar/$" NO_CACHE
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate" env=NO_CACHE
The above sets the env var NO_CACHE
(to the value 1
) when the URL-path /foo/bar/
(only) is requested and the Header
directive is then only applied when that env var is set (by the last argument env=NO_CACHE
).
Alternatively, if you are on Apache 2.4+ then you can use Apache <If>
expressions to directly target that URL-path only, negating the need to set an env var. For example:
<If "%{REQUEST_URI} =~ m#^/foo/bar/$#">
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
</If>
Another alternative, if you are on Apache 2.4.10+ then you can use an Apache expression directly in the Header
directive in order to target that URL-path only. For example:
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate" "expr=%{REQUEST_URI} =~ m#^/foo/bar/$#"