为啥 CodeIgniter 将 /index.php/ 添加到所有 URL?

Posted

技术标签:

【中文标题】为啥 CodeIgniter 将 /index.php/ 添加到所有 URL?【英文标题】:Why does CodeIgniter add /index.php/ to all URLs?为什么 CodeIgniter 将 /index.php/ 添加到所有 URL? 【发布时间】:2012-04-04 09:08:24 【问题描述】:

我的 CodeIgnitor 设置在任何 URL 上返回 404,除了基本 URL http://x.example.com/test/)。当我尝试访问诸如 http://x.example.com/test/foobar/177 之类的 URL 时,我得到以下 404:

找不到

在此服务器上找不到请求的 URL /index.php/foobar/177。

当我检查我的网络服务器日志(这是 Webfaction)时,它只是说:

[2012 年 3 月 19 日星期一 12:23:45] [错误] [客户端 12.34.56.78] 文件没有 存在:/var/www/html/no_app_on_root.htmlindex.php

文件路径与 index.php 连接的方式很奇怪,不是吗?

这是我的 .htaccess:

Options +FollowSymLinks

# Set which file that's fetched if only directory is specified
DirectoryIndex index.php index.html index.htm

# don't list category structures
Options -Indexes

# don't display .htaccess files
<Files .htaccess>
order allow,deny
deny from all
</Files>


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#‘system’ can be replaced if you have renamed your system folder.
RewriteCond %REQUEST_URI ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn’t true it sends the
#request to index.php
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
#This last condition enables access to the images and css folders, and the robots.txt file
#Submitted by Michael Radlmaier (mradlmaier)
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>


<IfModule !mod_rewrite.c>
# If we don’t have mod_rewrite installed, all 404’s
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin

ErrorDocument 404 /index.php
</IfModule>

我的路线.php:

$route['default_controller'] = "home";
$route['404_override'] = '';

$route['foobar/177'] = "foobar/foobar_177";
$route['foobar/:num/add'] = "foobar/add";
$route['foobar/:num/review'] = "foobar/review";
$route['foobar/:num'] = "foobar";

我的 config.php(是的,该站点位于子域的目录中):

$config['base_url'] = 'http://x.example.com/test/';

$config['index_page'] = '';

$config['uri_protocol'] = 'AUTO';

【问题讨论】:

试试这个***.com/questions/9455757/…。 【参考方案1】:

用于从 url 隐藏 index.php 的重写规则

RewriteRule ^(.*)$ /index.php/$1 [L]

将所有请求发送到 /index.php/$1

并且没有针对这些的处理程序,您需要明确告诉 Apache 您不希望某些 URL 被重定向到 index.php

【讨论】:

对不起,我听不懂。【参考方案2】:

在文件中:config.php

编辑这一行:

$config['uri_protocol'] = 'AUTO';

到:

$config['uri_protocol'] = 'QUERY_STRING';

您的 .htaccess 文件应如下所示:

RewriteEngine On
RewriteCond %REQUEST_URI ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

【讨论】:

我已经从 docroot 中移动了系统和应用程序文件夹,这有什么改变吗? 我继续尝试了两个更改。我仍然得到 404,但至少 index.php 没有插入 url:在此服务器上找不到请求的 URL /test/foobar/177。 这意味着 .htaccess 文件在 ROOT 中找不到或其中的代码无法运行。确保它。 我已经验证 .htaccess 是由 apache 处理的。

以上是关于为啥 CodeIgniter 将 /index.php/ 添加到所有 URL?的主要内容,如果未能解决你的问题,请参考以下文章

为啥不断在 codeigniter 上获得未定义的属性?

为啥/在找到页面时 CodeIgniter 在哪里设置 404 http 状态?

当我尝试访问我的 CodeIgniter 应用程序时,为啥会收到 404 错误?

CodeIgniter 404 页面未找到,但为啥?

为啥会出现 CodeIgniter 安装错误

为啥这个构造函数无法在 Codeigniter 中加载?