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

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.

图片转代码服务由CSDN问答提供 功能建议

所以,我正在使用我正在构建的PHP应用程序中使用url。 \ n

我正在使用.htaccess的mod_rewrite。 下面是这样的:

 &lt; IfModule mod_rewrite.c&gt; 
 RewriteEngine On 
 RewriteBase / 
 
 RewriteCond%{REQUEST_FILENAME}!-f 
  RewriteCond%{REQUEST_FILENAME}!-d 
 RewriteRule(。*)index.php / $ 1 
&lt; / IfModule&gt; 
   
 
 

我认为是否写了一个URL 如下所示:

  http://localdomain.com/controller
   
 
 

我希望'控制器'结束 没有问题作为$ _SERVER ['PATH_INFO']的一部分。 但是,在运行Apache 2.2.22的Ubuntu 12.04上,我得到404 Not Found错误:

  NOT FOUND 
未找到请求的URL /index.php/controller 服务器。
   
 
 

在index.php之后的斜杠后,似乎正在正确处理“控制器”,但有没有人知道我为什么要获得404而不是 预期的响应?

最终我试图捕获'控制器'以在我正在构建的MVC框架中使用。

在这种情况下 我有一个非常简单的index.php,只有以下内容:

 &lt;?php 
echo $ _SERVER ['PATH_INFO']; 
   
 
 

然而,我仍然得到404错误而不是预期的路径信息。

注意:转到 http://localdomain.com/ http:// localdomain .com / index.php 完美运作。 mod_rewrite打开并正常工作,AllowOverride设置为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));
    
    点赞 评论
  • dpuwov1487 2012-07-06 19:45

    Here's the .htaccess I created and use for my MVC :

    # Mod Rewrite
    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
    
        RewriteRule ^(.*)$          /root/index.php/$1 [PT,L]
    </IfModule>
    

    Try to add the ^ and $ + the [PT,L]

    点赞 评论
  • duannuci4008 2012-07-06 19:47

    If you're expecting the URL to be rewritten without the index.php in it then just use:

    RewriteRule ^(.*)$ index.php [NC,L]
    
    点赞 评论

相关推荐 更多相似问题