【问题标题】:Getting the Apache "ErrorDocument" path with PHP bootstrap configuration使用 PHP 引导配置获取 Apache“ErrorDocument”路径
【发布时间】:2016-11-30 19:32:23
【问题描述】:

我试图弄清楚为什么 Apache 不将 ErrorDocument 路径作为 PHP $_SERVER 变量传递。

采用以下 Apache VHOST 配置。所有请求都发送到“index.php”,除非请求在“/js”、“/media”或“/skin”中(很像 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]

假设用户尝试访问“/skin”目录中不存在的文件:“/skin/i/dont/exist.jpg”。

我的期望是,由于 Apache 发现此文件在物理上不存在,它会尝试(与根相关)向“/var/www/mydomain.com/public/www/core/error/notfound/ ”。它确实这样做了,并且通过引导程序正确处理了请求。但是,所有 $_SERVER 变量都表示最初请求的路径“/skin/i/dont/exist.jpg”,而不是 ErrorDocument 路径。

Apache 确实将“REDIRECT_STATUS”设置为 404,但似乎这是我知道存在问题的唯一方法。我希望我的引导程序 index.php 知道 Apache 实际上是在尝试请求“/core/error/notfound/”,因此它的路由正确。

我是否缺少 Apache 设置?我对这应该如何运作的期望是否不正确?我在 Ubuntu 上使用 PHP 7.0.8 和 Apache 2.4.18。

【问题讨论】:

    标签: php apache


    【解决方案1】:

    我知道出了什么问题。 RewriteCond 和 RewriteRule 行需要驻留在指令中。

    <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>
    

    PHP $_SERVER 变量现在按预期返回:

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

    也许如果有人读到这个,他们可以告诉我为什么添加指令会改变环境变量的行为?

    【讨论】:

      猜你喜欢
      • 2011-10-17
      • 2011-03-19
      • 1970-01-01
      • 2015-10-20
      • 2023-03-31
      • 1970-01-01
      • 2013-07-12
      • 2011-11-02
      • 1970-01-01
      相关资源
      最近更新 更多