duanpuluan0480 2012-07-06 19:44
浏览 124
已采纳

Apache / PHP url解析问题

So, I am playing with urls a bit with a PHP app I am building.

I am using mod_rewrite using .htaccess. Here is what that looks like:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) index.php/$1
</IfModule>

I thought if a URL was written like below:

http://localdomain.com/controller

I would expect 'controller' ending without issues as part of the $_SERVER['PATH_INFO']. However, on Ubuntu 12.04 running Apache 2.2.22 I get a 404 Not Found error instead:

NOT FOUND
The requested URL /index.php/controller was not found on this server.

It appears that 'controller' is being properly addressed after a slash following index.php, but Does anyone have any idea why I am getting a 404 instead of the expected response?

Ultimately I am trying to capture the 'controller' to use in an MVC framework I am building.

In this case I have a very simple index.php with ONLY the following:

<?php
echo $_SERVER['PATH_INFO'];

Yet, I still get a 404 error instead of the expected path info.

NOTE: Going to just http://localdomain.com/ or http://localdomain.com/index.php work perfectly. mod_rewrite is on and working, and AllowOverride is set to All.

  • 写回答

3条回答 默认 最新

  • douyong5825 2012-10-12 19:26
    关注

    If you're running apache 2.2.22, then your PHP code is now responsible for specifying the 200 response if the controller function was found.

    Apache 2.2.22 seems to take a different philosophy on rewrites compared to 2.2.14. Long story, but it makes sense. It assumes that if you're rewriting to a front-controller like CodeIgniter does, then the PHP code is responsible for deciding whether the requested URI was found or not. Apache waits for the PHP code to send header('HTTP/1.1 200 OK'), and if it doesn't then it sends header('HTTP/1.1 404 Not Found').

    Interestingly, the actual content coming from your controller is probably sent in the response body too. But many HTTP clients ignore that if the response code was 404 (like MSIE, wget, etc.)

    The short version is that apache 2.2.14 (on platinum) is optimistic, assuming 200 unless otherwise specified by PHP, whereas apache 2.2.22 is pesimistic, assuming 404 unless otherwise specified by PHP.

    So somewhere in your code, you must place:

    header('HTTP/1.1 200 OK');
    

    This should be placed just after the front-controller has determined that the URI is valid and a class and method exists and can be called for this URI, but before the class and method are actually called.

    In CodeIgniter 2.1.3, it looks like this:

    // Added this line for Apache 2.2.22
    header('HTTP/1.0 200 OK');
    // Call the requested method.
    // Any URI segments present (besides the class/function) will be passed to the method for convenience
    call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器