使用PHP引导程序配置获取Apache“ErrorDocument”路径

I'm trying to figure out why Apache doesn't pass the ErrorDocument path as a PHP $_SERVER variable.

Take the following Apache VHOST configuration. All requests are sent to "index.php" unless the request is within "/js", "/media", or "/skin" (much like Magento).

# Error documents
ErrorDocument 401 /core/error/notauthorized/
ErrorDocument 403 /core/error/notfound/
ErrorDocument 404 /core/error/notfound/
ErrorDocument 500 /core/error/internalservererror/
ErrorDocument 503 /core/error/serviceunavailable/

# index.php rewrite bootstrap
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/(js|media|skin)/
RewriteRule .* /var/www/mydomain.com/public/www/index.php [L]

Lets say that a user attempts to access a file within the "/skin" directory that doesn't exist: "/skin/i/dont/exist.jpg".

My expectation is that since Apache sees that this file physically doesn't exist, it attempts a request (relevant to the root) to "/var/www/mydomain.com/public/www/core/error/notfound/". It does do this and the request is handled properly through the bootstrap. However, all the $_SERVER variables indicate the originally requested path "/skin/i/dont/exist.jpg" and not the ErrorDocument path.

Apache does set "REDIRECT_STATUS" to 404, but it seems this would be the only way I know there was a problem. I want my bootstrap index.php to know that Apache is actually trying to request "/core/error/notfound/" so its routed correctly.

Am I missing an Apache setting? Are my expectations of how this should function incorrect? I'm using PHP 7.0.8 and Apache 2.4.18 on Ubuntu.

I figured out what was wrong. The RewriteCond and RewriteRule lines need to reside in the directive.

<Directory /var/www/mydomain.com/public/www>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
  RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]
  RewriteCond %{REQUEST_URI} !^/(js|media|skin)/
  RewriteRule .* /var/www/mydomain.com/public/www/index.php [L]
</Directory>

The PHP $_SERVER variables return as expected now:

["REQUEST_URI"]=>
string(13) "/skin/i/dont/exist.jpg"
["REDIRECT_URL"]=>
string(21) "/core/error/notfound/"
["SCRIPT_NAME"]=>
string(10) "/index.php"

Perhaps if someone reads this, they can tell me why adding the directive changes the environment variable behavior?