php CodeIgniter 404 页面未找到,但为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3752609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter 404 Page Not Found, but why?
提问by Joseph
I am using CodeIgniter for two applications (a public and an admin app). The important elements of the document structure are:
我将 CodeIgniter 用于两个应用程序(一个公共应用程序和一个管理应用程序)。文档结构的重要元素是:
/admin
/admin/.htaccess
/admin/index.html
/application
/application/admin
/application/public
/system
.htaccess
index.php
The /admin/.htaccess file looks like this:
/admin/.htaccess 文件如下所示:
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/ [L,QSA]
The /admin/index.php has the following changes:
/admin/index.php 有以下变化:
$system_folder = "../system";
$application_folder = "../application/admin"; (this line exists of course twice)
And the /application/admin/config/routes.php contains the following:
/application/admin/config/routes.php 包含以下内容:
$route['default_controller'] = "welcome";
$route['admin'] = 'welcome';
Welcome is my default controller.
欢迎是我的默认控制器。
When I call up the Domain/admin I get a 404 Page Not Found error. When I call up the Domain/admin/welcome everything works fine. In the debug logs I get the following error message:
当我调用域/管理员时,我收到 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
Weirdly enough this setup works perfectly on my local MAMP installation (with the localdomain/admin/), but when I publish and test it on the "live" server, I just get 404 errors.
奇怪的是,此设置在我的本地 MAMP 安装(使用 localdomain/admin/)上完美运行,但是当我在“实时”服务器上发布和测试它时,我只会收到 404 错误。
Any ideas? What am I doing wrong? Thanks C.
有任何想法吗?我究竟做错了什么?谢谢 C。
采纳答案by Joseph
The cause of the problem was that the server was running PHP using FastCGI.
问题的原因是服务器正在使用 FastCGI 运行 PHP。
After changing the config.php to
将 config.php 更改为
$config['uri_protocol'] = "REQUEST_URI";
everything worked.
一切正常。
回答by Amin Balogun
You could try one of two things or a combination of both.
您可以尝试两种方法中的一种或两者的组合。
- Be sure that your controller's name starts with a capital letter. eg "Mycontroller.php"
- If you have not made any changes to your route, for some strange reason, you might have to include capital letters in your url. e.g if your controller is 'Mycontroller.php' with a function named 'testfunc' inside it, then your url will look like this: "http://www.yourdomain/index.php/Mycontroller/testfunc". Note the capital letter. (I'm assuming you haven't added the htaccess file to remove the 'index.php' part. If you have, just remove it from the url.)
- 确保您的控制器名称以大写字母开头。例如“Mycontroller.php”
- 如果您没有对您的路线进行任何更改,出于某种奇怪的原因,您可能必须在您的网址中包含大写字母。例如,如果您的控制器是“Mycontroller.php”,其中包含一个名为“testfunc”的函数,那么您的 url 将如下所示:“ http://www.yourdomain/index.php/Mycontroller/testfunc”。注意大写字母。(我假设您没有添加 htaccess 文件来删除“index.php”部分。如果有,只需从 url 中删除它。)
I hope this helps someone
我希望这可以帮助别人
回答by JoshuaC
Leaving this answer here for others who ran into my situation.
将这个答案留在这里给遇到我情况的其他人。
My codeigniter app was working fine in localhost/WAMP, but was unable to route and produced 404 not found errors when pushing to an AWS EC2 instance. My issue was solved from the answer from HEREhtaccess works in localhost but doesn't work in EC2 instance
我的 codeigniter 应用程序在 localhost/WAMP 中运行良好,但在推送到 AWS EC2 实例时无法路由并产生 404 not found 错误。我的问题已从HERE htaccess 在 localhost 中工作但在 EC2 实例中不起作用的答案中得到解决
(route to my admin page) {domain}/admin was producing 404
(路由到我的管理页面){domain}/admin 正在生成 404
the /etc/httpd/conf/httpd.conf
file needs to be modified.
该/etc/httpd/conf/httpd.conf
文件需要修改。
-after every instance of "DocumentRoot "/var/www/html"" (2 places) "AllowOverride None" needed to be changed to "AllowOverride All".
- 在“DocumentRoot”/var/www/html””(2 个地方)的每个实例之后,“AllowOverride None”需要更改为“AllowOverride All”。
Restarted the EC2 instance from the AWS dashboard.
从 AWS 控制面板重新启动 EC2 实例。
{domain}/admin is now accessible and working as intended.
{domain}/admin 现在可以访问并按预期工作。
hope this helps someone else like it helped me!
希望这可以帮助其他人,因为它帮助了我!
回答by DevYuan
- Change your controller name first letter to uppercase.
- Change your url same as your controller name.
- 将您的控制器名称第一个字母更改为大写。
- 更改与您的控制器名称相同的网址。
e.g:
例如:
Your controller name is YourController
您的控制器名称是 YourController
Your url must be:
您的网址必须是:
http://example.com/index.php/YourController/method
http://example.com/index.php/YourController/method
Not be:
不是:
回答by MAK
we have to give the controller name in lower cases in server side
我们必须在服务器端以小写形式给出控制器名称
$this->class = strtolower(__CLASS__);
回答by treeface
Your folder/file structure seems a little odd to me. I can't quite figure out how you've got this laid out.
你的文件夹/文件结构对我来说似乎有点奇怪。我不太明白你是如何布置这个的。
Hello I am using CodeIgniter for two applications (a public and an admin app).
您好,我将 CodeIgniter 用于两个应用程序(一个公共应用程序和一个管理应用程序)。
This sounds to me like you've got two separate CI installations. If this is the case, I'd recommend against it. Why not just handle all admin stuff in an admin controller? If you do want two separate CI installations, make sure they are definitely distinct entities and that the two aren't conflicting with one another. This line:
这听起来像你有两个独立的 CI 安装。如果是这种情况,我建议不要这样做。为什么不在管理控制器中处理所有管理内容?如果您确实想要两个单独的 CI 安装,请确保它们绝对是不同的实体,并且两者不会相互冲突。这一行:
$system_folder = "../system";
$application_folder = "../application/admin"; (this line exists of course twice)
And the place you said this exists (/admin/index.php...or did you mean /admin/application/config?) has me scratching my head. You have admin/application/admin and a system folder at the top level?
你说这个存在的地方(/admin/index.php ...或者你的意思是/admin/application/config?)让我摸不着头脑。您在顶层有 admin/application/admin 和系统文件夹?
回答by Antony P.
I had the same issue after migrating to a new environment and it was simply that the server didn't run mod_rewrite
迁移到新环境后我遇到了同样的问题,只是服务器没有运行 mod_rewrite
a quick sudo a2enmod rewrite
then sudo systemctl restart apache2
sudo a2enmod rewrite
然后很快sudo systemctl restart apache2
and problem solved...
和问题解决了...
Thanks @fanis who pointed that out in his comment on the question.
感谢@fanis,他在对这个问题的评论中指出了这一点。
回答by Rizwan Syed
If your application is in sub-folder then the Folder name in directory and URL must be same (case-sensitive).
如果您的应用程序位于子文件夹中,则目录和 URL 中的文件夹名称必须相同(区分大小写)。
回答by NAVNEET CHANDAN
It happens cause of multiple reasons but the answer missing above there's the "className" while extending your controller.
它发生的原因有多种,但上面缺少的答案是扩展控制器时的“className”。
Make sure your class name is the same as your controller name is your controllers. e.g., If your controller name is Settings.php, you must extend the controller like.
确保您的类名与您的控制器名称相同。例如,如果您的控制器名称是 Settings.php,您必须像这样扩展控制器。
class Settings extends CI_Controller
{
// some actions like...
public function __construct(){
// and so and so...
}
}
回答by Dan Linh
If you installed new Codeigniter, please check if you added .htaccess file on root directory. If you didn't add it yet, please add it. You can put default content it the .htaccess file like below.
如果您安装了新的 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?/ [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?/ [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?/ [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>