CodeIgniter 404页面未找到,但为什么?

24

我在使用CodeIgniter来开发两个应用程序(一个公共的,一个管理的)。 文档结构的重要元素包括:

/admin
/admin/.htaccess
/admin/index.html
/application
/application/admin
/application/public
/system
.htaccess
index.php

/admin/.htaccess文件看起来像这样:

DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

/admin/index.php页面有以下更改:

$system_folder = "../system";
$application_folder = "../application/admin"; (this line exists of course twice)

同时/application/admin/config/routes.php文件包含以下内容:

$route['default_controller'] = "welcome";
$route['admin'] = 'welcome';

欢迎页是我的默认控制器。

当我访问Domain/admin时,我会遇到404页面未找到错误。但是当我访问Domain/admin/welcome时一切都正常。在调试日志中,我得到以下错误信息:

DEBUG - 2010-09-20 16:27:34 --> Config Class Initialized
DEBUG - 2010-09-20 16:27:34 --> Hooks Class Initialized
DEBUG - 2010-09-20 16:27:34 --> URI Class Initialized
ERROR - 2010-09-20 16:27:34 --> 404 Page Not Found --> admin

奇怪的是,在我的本地MAMP安装中(使用localdomain/admin/),这个设置完美运行,但是当我在“实际”服务器上发布和测试时,我只收到404错误。

有什么想法吗?我做错了什么? 谢谢 C.


只是一个想法,也许实时服务器没有运行 mod_rewrite?实时服务器只会给你 404 或偶尔的 404 吗? - Fanis Hatzidakis
它似乎正在工作 - 我刚刚使用捕获所有非www域名并重定向到google.com进行了测试。404错误明显是由CI生成的(因为它们使用CSS)。 - Joseph
只是想提醒一下,如果您正在运行 Codeigniter 3 的新安装,并且主机文件夹名称中有空格,则会出现404错误。版本3.0.0。 - Vael Victus
12个回答

14
问题的原因是服务器使用FastCGI运行PHP。更改config.php后,问题得到解决。
$config['uri_protocol'] = "REQUEST_URI";

一切都正常运作。


路由已经开始工作,但现在命令行界面返回欢迎使用CodeIgniter屏幕。有什么办法可以解决吗?我在这里发布了更多信息。 - Kuf

14

你可以尝试以下两种方法或两种方法的组合。

  1. 确保您的控制器名称以大写字母开头。例如"Mycontroller.php"
  2. 如果您没有对路由进行任何更改,由于某种奇怪的原因,您可能需要在URL中包含大写字母。例如,如果您的控制器是'Mycontroller.php',其中包含名为'testfunc'的函数,则您的URL将如下所示:" http://www.yourdomain/index.php/Mycontroller/testfunc"。请注意大写字母。(我假设您还没有添加htaccess文件来删除'index.php'部分。如果您已经添加了,请从URL中删除它。)

希望这能帮助到有需要的人。


3
控制器确实需要大写,但是通过URL访问它并不一定需要在URL中使用大写字母。 - logicbloke

11
留下这个答案是为了帮助遇到和我一样情况的人。
我的codeigniter应用在本地主机/WAMP上运行良好,但是在推送到AWS EC2实例时无法路由并产生404未找到错误。我的问题得到了这里 htaccess在本地主机中有效但在EC2实例中无效的答案解决。
(路由到我的管理页面) {domain}/admin 产生404
需要修改/etc/httpd/conf/httpd.conf文件。
-每次出现"DocumentRoot "/var/www/html""(2个位置)后,“AllowOverride None”需要更改为“AllowOverride All”。
从AWS仪表板重新启动EC2实例。
{domain}/admin现在可以访问并按预期工作。
希望这能帮助像我一样的其他人!

7

3
如果您安装了新的Codeigniter,请检查是否在根目录上添加了.htaccess文件。 如果尚未添加,请添加。 您可以将以下默认内容放入.htaccess文件中。
<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]

#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
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
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>

2

按照以下方式修改 Apache 配置文件:

sudo nano /etc/apache2/apache2.conf 

跳转到第172行并修改

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

为了

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

并最后
sudo systemctl restart apache2

2

在服务器端,我们必须使用小写字母来命名控制器。

$this->class = strtolower(__CLASS__);

1

在迁移到新环境后,我遇到了同样的问题,原因很简单,服务器没有运行mod_rewrite。

快速执行以下命令:sudo a2enmod rewrite 然后执行sudo systemctl restart apache2

问题解决...

感谢@fanis在评论中指出这一点。


1

你的文件夹/文件结构对我来说有点奇怪。我无法弄清楚你是如何布置的。

你好,我正在使用CodeIgniter开发两个应用程序(一个公共应用程序和一个管理应用程序)。

这听起来像是你有两个独立的CI安装。如果是这样,我建议不要这样做。为什么不在一个管理控制器中处理所有管理工作呢?如果你确实想要两个独立的CI安装,请确保它们是完全独立的实体,并且两者之间没有冲突。这一行:

$system_folder = "../system";
$application_folder = "../application/admin"; (this line exists of course twice)

你说的这个地方存在吗(/admin/index.php...或者你是指/admin/application/config)让我感到困惑。你在顶层有admin/application/admin和一个system文件夹?


这实际上是我构建应用程序的方式。我有一个管理应用程序和一个Web应用程序。它们都共享一个系统文件夹('../system'),并且每个应用程序都有一个单独的应用程序文件夹('../application/admin'),('../application/website')。 - Matthew
它基于这个链接:http://codeigniter.com/user_guide/general/managing_apps.html 只有一个系统文件夹。我将应用程序文件夹移动到与系统文件夹相同的级别。 - Joseph
我的意思是在 /admin/index.php 文件中,我已经更改了第 26、43 和 101 行。 - Joseph

1
在我的情况下,我正在使用它在本地主机上,并忘记在.htaccess中更改RewriteBase。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接