<location>和部分不存在的目录?

时间:2017-03-22 10:19:19

标签: apache2

在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/ *。

1 个答案:

答案 0 :(得分:0)

这是因为你的倒数第二次重写不匹配,但是你的上一次重写没有,导致你的请求在mod_headers过滤器运行时不再是for / foo /吗?