dtjo87679 2016-07-27 00:32
浏览 167

使用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.

  • 写回答

1条回答 默认 最新

  • douweibeng5219 2016-07-27 23:21
    关注

    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?

    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源