大网数据广告活动
服务器运维问答

Apply Apache httpd configuration to exact file and no subdirectories

· 52CE 我爱测

I have a problem that it seems should be simple. I want to apply a group of configuration directives to a single file (in this case myfile.html in the DocumentRoot). There may also be subdirectories containing myfile.html , these…

问题原文

原帖链接 · amoe · CC BY-SA 3.0

I have a problem that it seems should be simple. I want to apply a group of configuration directives to a single file (in this case myfile.html in the DocumentRoot). There may also be subdirectories containing myfile.html, these should not get the configuration. I also want to do it without using Location or LocationMatch.

I came up with the following

DocumentRoot "/home/amoe/opt/httpd/htdocs"
<DirectoryMatch "^/home/amoe/opt/httpd/htdocs$">
    <Files "myfile.html">
        ExpiresActive on
        ExpiresDefault A10
    </Files>
</DirectoryMatch>

However, the Expires header doesn't get set when requesting /myfile.html. Adding a trailing slash on the DirectoryMatch seems to make no difference.

You can assume that I want to do this without Location for academic reasons. (Edit: Also, to quote the documentation, It is important to never use when trying to restrict access to objects in the filesystem.)

已采纳解答

原帖链接 · MoeBrowne · CC BY-SA 3.0

The only way I have found to make this work is by using the following:

<DirectoryMatch ^/home/amoe/opt/httpd/htdocs/[^/]+$>
    <Files "test.html">
        ExpiresActive on
        ExpiresDefault A10
    </Files>
</DirectoryMatch>

I believe that the extra [^/]+ shouldn't be required to get this to work, see below for the full explanation of how I came to this conclusion, and as such I believe this is a bug in Apache.


All of the following is based on an Ubuntu 16.04.1 LTS environment running Apache 2.4.23 with test files in these locations:

  • /path/test/test.html
  • /path/test/noheader-test.html
  • /path/test/child/test.html
  • /path/test/child/grandchild/test.html

Firstly I tried a number of different regex variants, all of which failed to match:

<DirectoryMatch ^/path/test$>
<DirectoryMatch ^/path/test/$>
<DirectoryMatch /path/test/$>
<Directory ~ ^/path/test$>
<Directory ~ ^/path/test/$>
<Directory ~ /path/test/$>

I found however that if you omit the EOL anchor ($) then it matches:

<DirectoryMatch ^/path/test/>
    <Files "test.html">
        ExpiresActive on
        ExpiresDefault A10
    </Files>
</Directory>

However this ofcourse matches the test.html files in all the subdirectories:

  • /path/test/test.html - Expires header sent
  • /path/test/noheader-test.html - no Expires header sent
  • /path/test/child/test.html - Expires header sent
  • /path/test/child/grandchild/test.html - Expires header sent

It was as though there was something else in the path that the regex wasn't matching because when I changed the regex to ^/your/path/test/.+$ it matched.

Wanting to know what this missing part of the path was I used a regex capture group and the Header directive to get Apache to tell me what was missing, like so:

<DirectoryMatch ^/path/test/(?<MISSINGPART>.+)$>
    <Files "test.html">
        Header add X-TEST "expr=%{env:MATCH_MISSINGPART}"
    </Files>
</DirectoryMatch>

This gave the following headers:

  • /path/test/test.html - x-test:"test.html"
  • /path/test/noheader-test.html - no header sent
  • /path/test/child/test.html - x-test:"child/test.html"
  • /path/test/child/grandchild/test.html - x-test:"child/grandchild/test.html"

This suggests that DirectoryMatch is matching the file name too?!

On the Apache bug tracker bug 41867 somewhat fits this description so may explain this (I also tested this without the nested <Files> directive and the header values remained the same)

So to prove it I tried substituting test.html into the regex like so:

<DirectoryMatch "^/path/test/test.html$">
    <Files "test.html">
        Header add X-TEST "Match"
    </Files>
</DirectoryMatch>

No X-TEST header was returned.

This did however mean that the regex could be made to match anything that isn't a directory after the directory we're interested in matching, like so:

<DirectoryMatch "^/path/test/[^/]+$">
    <Files "test.html">
        ExpiresActive on
        ExpiresDefault A10
    </Files>
</DirectoryMatch>

This gives the header in all the desired places:

  • /path/test/test.html - Expires header sent
  • /path/test/noheader-test.html - no Expires header sent
  • /path/test/child/test.html - no Expires header sent
  • /path/test/child/grandchild/test.html - no Expires header sent

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-10-24)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。