在Apache 2.2和2.4中,<Location /foo>
指令和请求的URL http://example.com/foo/non-existing不匹配,我得到奇怪的行为。
说我的配置是:
<VirtualHost *:80>
DocumentRoot "/var/www/apache.test"
ServerName apache.test
<Directory "/var/www/apache.test">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
# Catch non-existing resources with index.php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}/index.php [L]
</Directory>
<Location "/">
Header set X-All "true"
</Location>
<Location "/foo">
Header set X-Foo "true"
</Location>
</VirtualHost>
和 DocumentRoot 目录/var/www/apache.test
包含以下内容:
/var/www/apache.test
├── bar
│ └── baz
├── foo
│ └── qux
└── index.php
请注意/foo
目录实际存在于 DocumentRoot 中,而目录/foo/non-existing
则不存在。
使用以下代码测试各种网址:curl -D - http://apache.test/ -o /dev/null -s | grep -F "X-"
我得到以下结果:
URL | X-All? | X-Foo?
-----------------------|--------|-------
/ | X |
/bar/ | X |
/bar/baz/ | X |
/bar/non-existing/ | X |
/non-existing/ | X |
/foo/ | X | X
/foo/qux/ | X | X
/foo/non-existing/ | X |
/foo/qux/non-existing/ | X |
我认为<Location "/foo">
会为http://apache.test/foo/ *。
我的<Directory>
用于文件系统项,<Location>
用于动态网址。所以我也尝试使用<Directory>
和<Location>
来涵盖所有情况:
<VirtualHost *:80>
DocumentRoot "/var/www/apache.test"
ServerName apache.test
<Directory "/var/www/apache.test">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
# Catch non-existing resources with index.php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}/index.php [L]
Header set X-AllDir "true"
</Directory>
<Location "/">
Header set X-AllLoc "true"
</Location>
<Directory "/var/www/apache.test/foo">
Header set X-FooDir "true"
</Location>
<Location "/foo">
Header set X-FooLoc "true"
</Location>
</VirtualHost>
我得到以下结果:
URL | X-AllDir? | X-AllLoc? | X-FooDir? | X-FooLoc?
-----------------------|-----------|-----------|-----------|----------
/ | X | X | |
/bar/ | X | X | |
/bar/baz/ | X | X | |
/bar/non-existing/ | X | X | |
/non-existing/ | X | X | |
/foo/ | X | X | X | X
/foo/qux/ | X | X | X | X
/foo/non-existing/ | X | X | |
/foo/qux/non-existing/ | X | X | |
我也尝试过只有<Location>
专用于“/ foo / non-existing /”:
<VirtualHost *:80>
DocumentRoot "/var/www/apache.test"
ServerName apache.test
<Directory "/var/www/apache.test">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
# Catch non-existing resources with index.php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}/index.php [L]
</Directory>
<Location "/foo/non-existing">
Header set X-FooNonExisting "true"
</Location>
</VirtualHost>
但http://apache.test/foo/non-existing仍然不会返回X-FooNonExisting
标题。
我看到它的方式,Apache无法将请求与/foo/non-existing/
匹配,因为要求的资产既是现有目录(“/ foo”)又是虚拟子目录(“不存在”)。 / p>
我的最终目标是在所有HTTP响应中设置一个给定的标头(对于此VirtualHost
)但是回答请求的人
http://example.com/foo/qux/non-existing/ *。
答案 0 :(得分:0)
这是因为你的倒数第二次重写不匹配,但是你的上一次重写没有,导致你的请求在mod_headers过滤器运行时不再是for / foo /吗?