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?

    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名