Apache/PHP url解析问题

Apache/PHP url解析问题,php,apache,mod-rewrite,Php,Apache,Mod Rewrite,所以,我正在用我正在构建的一个PHP应用程序来处理URL 我正在使用.htaccess使用mod_重写。下面是它的样子: <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule (.*) index.php/$1 <

所以,我正在用我正在构建的一个PHP应用程序来处理URL

我正在使用.htaccess使用mod_重写。下面是它的样子:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) index.php/$1
</IfModule>
我希望作为$\u服务器['PATH\u INFO']的一部分,“controller”结束时不会出现问题。然而,在运行Apache2.2.22的Ubuntu 12.04上,我得到了一个404 Not Found错误:

NOT FOUND
The requested URL /index.php/controller was not found on this server.
看起来“controller”在index.php后面的斜杠后面得到了正确的地址,但是有人知道为什么我得到的是404而不是预期的响应吗

最终,我试图捕获“控制器”,以便在我正在构建的MVC框架中使用

在本例中,我有一个非常简单的index.php,它只包含以下内容:

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

以下是我为MVC创建和使用的.htaccess:

# Mod Rewrite
<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^(.*)$          /root/index.php/$1 [PT,L]
</IfModule>
#修改重写
重新启动发动机
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)$/root/index.php/$1[PT,L]

尝试添加
^
$
+
[PT,L]

如果您希望在其中不包含index.php的情况下重写URL,则只需使用:

RewriteRule ^(.*)$ index.php [NC,L]

如果您运行的是Apache2.2.22,那么您的PHP代码现在负责在找到控制器函数时指定200响应

与2.2.14相比,Apache2.2.22在重写方面似乎采取了不同的理念。说来话长,但有道理。它假设如果您像CodeIgniter那样重写到前端控制器,那么PHP代码负责决定是否找到请求的URI。Apache等待PHP代码发送头('HTTP/1.1 200 OK'),如果不发送,则发送头('HTTP/1.1 404 Not Found')

有趣的是,来自控制器的实际内容可能也会发送到响应体中。但是许多HTTP客户机忽略了如果响应代码是404(如MSIE、wget等)

简短的版本是Apache2.2.14(白金版)是乐观的,假设200,除非PHP另有规定,而Apache2.2.22是悲观的,假设404,除非PHP另有规定

因此,在代码的某个地方,您必须放置:

header('HTTP/1.1 200 OK');
这应该放在前端控制器确定URI有效且存在类和方法并且可以为此URI调用该类和方法之后,但在实际调用该类和方法之前

在CodeIgniter 2.1.3中,如下所示:

// 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));

这似乎没有帮助。我仍然得到相同的404。好像Apache根本不允许重写工作,但我已经测试过了,确实如此。您在
httpd.conf
中有
AllowOverride all
?您是否安装了
mod_rewrite
?这不是
.htaccess
文件的故障,因为我知道我的工作正常。问题来自Apache2或其中的某个地方,但不是文件。这很奇怪,所以我把问题贴在这里。是的,AllowOverride设置为All,并且还安装了mod_rewrite。我在开发环境中安装了许多其他运行CakePHP或Zend Framework的站点,它们都运行良好。我知道我一定错过了什么。@AdamCulp是的,我同意这一定是个愚蠢的原因。检查mod_rewrite的日志。
// 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));