【问题标题】:CodeIgniter 404 Page Not Found, but why?CodeIgniter 404 页面未找到,但为什么?
【发布时间】:2011-04-14 17:38:34
【问题描述】:

我将 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';

Welcome 是我的默认控制器。

当我调用域/管理员时,我收到 404 Page Not Found 错误。当我调用域/管理员/欢迎时,一切正常。在调试日志中,我收到以下错误消息:

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还是偶尔给你?
  • 它似乎工作正常 - 我刚刚通过捕获所有非 www 域并重定向到 google.com 对其进行了测试。 404 错误肯定是由 CI 生成的(因为它们使用 CSS)。
  • 只是想发布一下,如果您正在运行 Codeigniter 3 的新安装并且您在托管文件夹的名称中有一个空格,它将提供 404。版本 3.0.0

标签: php codeigniter routes http-status-code-404


【解决方案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'),并且每个都有一个单独的应用程序文件夹('../application/admin'),('../application/website')。
  • 基于此:codeigniter.com/user_guide/general/managing_apps.html系统文件夹只有一个。我将应用程序文件夹移动到与系统文件夹相同的级别。
  • 我的意思是在 /admin/index.php 文件中我更改了第 26、43 和 101 行
【解决方案2】:

问题的原因是服务器使用 FastCGI 运行 PHP。

把config.php改成

$config['uri_protocol'] = "REQUEST_URI";

一切正常。

【讨论】:

  • 路由开始工作,但现在 CLI 返回Welcome to CodeIgniter 屏幕。反正周围呢?我已经发布了更多信息here
【解决方案3】:

我们必须在服务器端以小写形式给出控制器名称

$this->class = strtolower(__CLASS__);

【讨论】:

    【解决方案4】:

    在我的情况下,我在 localhost 上使用它并忘记在 .htaccess 中更改 RewriteBase

    【讨论】:

      【解决方案5】:

      将此答案留给遇到我情况的其他人。

      我的 codeigniter 应用程序在 localhost/WAMP 中运行良好,但在推送到 AWS EC2 实例时无法路由并产生 404 not found 错误。我的问题从HEREhtaccess works in localhost but doesn't work in EC2 instance的回答中得到了解决

      (路由到我的管理页面) {domain}/admin 产生 404

      /etc/httpd/conf/httpd.conf 文件需要修改。

      -在“DocumentRoot”/var/www/html“”的每个实例之后(2 个位置)“AllowOverride None”需要更改为“AllowOverride All”。

      从 AWS 控制面板重新启动 EC2 实例。

      {domain}/admin 现在可以访问并按预期工作。

      希望这能帮助像它帮助我一样的人!

      【讨论】:

        【解决方案6】:

        您可以尝试两种方法中的一种或两种方法的组合。

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

        我希望这对某人有所帮助

        【讨论】:

        • Controller 确实需要大写,但是通过 URL 访问它并不一定要在 URL 中输入大写字母。
        【解决方案7】:
        1. 将控制器名称的第一个字母更改为大写。
        2. 将您的网址更改为与您的控制器名称相同。

        例如:

        您的控制器名称是 YourController

        您的网址必须是:

        http://example.com/index.php/YourController/method

        不是:

        http://example.com/index.php/yourcontroller/method

        【讨论】:

          【解决方案8】:

          迁移到新环境后我遇到了同样的问题,只是服务器没有运行 mod_rewrite

          快速sudo a2enmod rewrite 然后sudo systemctl restart apache2

          问题解决了……

          感谢@fanis 在他对该问题的评论中指出了这一点。

          【讨论】:

            【解决方案9】:

            如果您的应用程序位于子文件夹中,则目录和 URL 中的文件夹名称必须相同(区分大小写)

            【讨论】:

              【解决方案10】:

              它发生的原因有多种,但上面缺少的答案是扩展控制器时的“className”。

              确保您的类名称与您的控制器名称相同。例如。, 如果你的控制器名称是 Settings.php,你必须像这样扩展控制器。

              class Settings extends CI_Controller
              {
              // some actions like...
                   public function __construct(){
                   // and so and so...
                   }
              }
              

              【讨论】:

                【解决方案11】:

                如果您安装了新的 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>
                

                【讨论】:

                  【解决方案12】:

                  修改 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
                  

                  【讨论】:

                    猜你喜欢
                    • 2015-07-28
                    • 1970-01-01
                    • 1970-01-01
                    • 2015-05-19
                    • 1970-01-01
                    • 2015-03-30
                    • 2018-05-13
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多