在文档根目录中,我添加了一个.htaccess文件:
ErrorDocument 404 /error.php
由于这是在.htaccess文件中,Apache在相对path中查找error.php,所以我可以在不同的子文件夹中放入不同的error.php文件,以使其执行不同的error.php:
request /app1/not-exists.txt : yields /app1/error.php request /app2/not-exists.txt : yields /app2/error.php request /not-exists/not-exists.txt : yields /error.php
这是期望的行为。 然而,
request /not-exists/app1/not-exists.txt : yields /app1/error.php request /app2/app1/not-exists.txt : yields /app1/error.php
这似乎不像预期的行为。 我期望:
request /not-exists/app1/not-exists.txt : yields /error.php request /app2/app1/not-exists.txt : yields /app2/error.php (or maybe /error.php)
或者最糟糕的是,一些通用的Apacheerror handling。 我误解了Apache应该在这里做什么? 文档似乎没有说清楚。
我认为你们的误解是相对的path。
.htaccess文件没有任何导致path相对的特殊行为; 就configuration行为而言,它们与<Directory>块本质上是一样的。
ErrorDocument没有任何上下文的概念; 当你input一个像/error.php这样的path时,总是假设它是相对于文档根目录的,不pipe它在哪里configuration。 在<Directory>块或.htaccess文件中的mod_rewriteconfiguration使用相对path,这可能是您正在考虑的行为。
有几个选项可以实现这个..你可以有一个error.php ,根据请求path从每个应用程序的错误文件中提取内容?
或者你可以使用mod_rewrite来获得所需的错误页面select行为(尽pipe逻辑匹配你所寻找的东西有点复杂)
<Directory /path/to/docroot> # First, we'll have a rule that will handle looking for an error.php in # the app directory that the request is in... # Not a file or a directory that exists: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Capture the application directory that we're trying to load from (if # there is one) for use in the next rule: RewriteCond %{REQUEST_FILENAME} ^/path/to/docroot/([^/]+)/ # Check if an error.php exists in that directory; RewriteCond /path/to/docroot/%1/error.php -f # Send the response to the error page. It should set the 404 response code. RewriteRule ^([^/]+)/.*$ $1/error.php [L] # Ok, the previous pile of rules didn't apply; if the user's requesting # a nonexistent resource, then either the user's not in an app subdirectory, # or they're in a subdirectory that didn't have an error.php. # So, this is a lot simpler: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ error.php [L] </Directory>