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 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来