【问题标题】:Codeigniter view is available but still receive page not found errorCodeigniter 视图可用,但仍收到找不到页面错误
【发布时间】:2015-05-22 03:12:42
【问题描述】:

我多次测试我的网站,但我无法解决“找不到页面”错误。 嗯,

1 步:我创建一个控制器并调用welcomelogin.php - 我存储两个函数

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class WelcomeLogin extends CI_Controller {
public function index()
{
    $this->home();
}
public function home(){

    $this->load->helper('url');
    $this->load->view('login');
}

public function inside(){
    $this->load->helper('url');
    $this->load->view('inside');    
}

2- 步骤:在我的 rote.php 文件中,我简单地执行以下操作:

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

3- 步骤:在视图文件夹中我创建了两个文件,一个是 login.php,第二个是 inside.php,其中 login.php 包含登录表单, inside.php 包含简单的 html。

现在我将登录页面作为我网站的主页,它工作正常: http://localhost/website/Codeigniter_Project/MyProject/

当我执行以下操作时,我得到页面未找到错误: 我尝试了两种方法,都给了我同样的错误:

第一种方式:直接给出视图名称和结果页面未找到 本地主机/网站/Codeigniter_Project/MyProject/inside

第二种方式:通过给出控制器名称然后查看但未找到结果页面 localhost/website/Codeigniter_Project/MyProject/welcomelogin(controllername)/inside

请帮助我,因为我花了将近 2 个小时搜索和应用不同的答案,是的,堆栈上有类似的问题,有些答案我已经尝试过但没有奏效,这些问题与我的有点不同。谢谢你们的时间。:) 我在 localhost 服务器上运行,我的 apache mod_rewrite 已打开。谢谢。 &我是codeigniter的新手,谢谢

我的 .htaccess 文件:

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /website/Codeigniter_Project/MyProject/


    # Removes trailing slashes (prevents SEO duplicate content issues)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ $1 [L,R=301]

    # Enforce www
    # If you have subdomains, you can add them to 
    # the list using the "|" (OR) regex operator
    RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
    RewriteRule ^(.*)$ http://www.domain.tld/$1 [L,R=301]

    # Enforce NO www
    #RewriteCond %{HTTP_HOST} ^www [NC]
    #RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]

    ###

    # 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
    RewriteRule ^(.*)$ index.php/$1 [L]

</IfModule>

<IfModule !mod_rewrite.c>

    # Without mod_rewrite, route 404's to the front controller
    ErrorDocument 404 /index.php

</IfModule>

【问题讨论】:

  • base_url 呢?
  • inside - 不工作

标签: php codeigniter


【解决方案1】:

请用这个网址试试

http://localhost/website/Codeigniter_Project/MyProject/index.php/inside

如果您没有使用.htaccess 限制url 的流量,希望这将起作用

【讨论】:

  • 您好,无法正常工作,我的 .htaccess 文件包含:全部拒绝,如果是,您认为我应该在那里更改任何内容,那么您能否提供代码,因为不太确定那里可能有什么。谢谢
  • @user2268488,我正在使用来自farinspace.com/codeigniter-htaccess-filehtaccess。它包含您需要的所有必要信息:)
  • 嗨,兄弟,我用上面的方法实现了我的 .htaccess 文件,但它似乎不起作用,因为我尝试给出控制器名称/方法名称,其中方法名称包含 $this->load->view() 但是还是不行:(
  • 您错过了 Rewritebase 值。根据您的代码,它将是/website/Codeigniter_Project/MyProject/
【解决方案2】:

PHP CI 代码看起来不错,你应该检查.htaccess 文件,

当您添加.htaccess 文件时,您应该将RewriteBase 从您的网络目录添加到项目主页,请参见下面的示例代码

RewriteBase /website/Codeigniter_Project/MyProject/

更新的 htaccess 文件

<IfModule mod_rewrite.c>
    RewriteEngine On
    ########### TODO: deploying on subdir must rewrite this
    RewriteBase  /website/Codeigniter_Project/MyProject/

    #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]

    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

    # you could simply throw this into an htaccess file the directory you want displayed
    Options -Indexes

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

【讨论】:

  • 嗨兄弟你能告诉我我必须把我的控制器名称放在.htaccess文件中吗:#如果你的默认控制器不是#“欢迎”,你可能应该改变这个RewriteRule ^(欢迎(/index)?|index(\.php)?)/?$ / [L,R=301]
  • 所以我必须用小号或大写替换为我的控制器名称welcomelogin,而不是welcome(/index),因为我有类WelcomeLogin
  • 好的,删除 .htaccess 中的 ### Canonicalize codeigniter URLs 部分,ci route 自动检测它...命名约定可能会影响操作系统版本,您可以在此处阅读更多信息 ellislab.com/codeigniter/user-guide/general/styleguide.html
  • 您好,我更新了我的 .htaccess 文件,您可以看看
  • 您应该使用初始 htaccess 文件,请参阅答案更新部分
【解决方案3】:

在我的情况下不需要任何修改,只需要在路径中使用 index.php 文件,所以路径应该是这样的http://localhost/website/Codeigniter_Project/MyProject/index.php/welcomelogin/inside

我通过一个教程找到了这个答案,我在研究我的在线答案链接时发现了这个链接是http://tutorialcodeigniter.com/beginners/creating-controller.html - 关于如何从 url 中删除 index.php。最好的解释和为我工作 - Cannot remove index.php from CodeIgniter URL

但无论如何,感谢那些试图帮助我查询的人。:)

【讨论】:

    猜你喜欢
    • 2015-07-27
    • 2016-12-06
    • 2015-10-07
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    • 2016-12-11
    • 2015-03-30
    • 1970-01-01
    相关资源
    最近更新 更多