【问题标题】:Codeigniter URI routing not working 404 errorCodeigniter URI 路由不起作用 404 错误
【发布时间】:2016-07-23 17:07:36
【问题描述】:

我的看法是:-

<li class="">
  <a class="" href="<?php echo base_url(); ?>Login"> 
  <i class="fa fa-unlock-alt"></i>&nbsp;Login </a>
</li>

我的登录控制器

class Login extends CI_Controller {
    public function __construct(){
        parent::__construct();
    }
    public function index()
    {
         $this->load->view('login');
    }
}

当我点击链接时,它显示 404 页面未找到。 但是当手动编辑链接并编写 /index.php/Login 时,它工作正常。如何解决这个问题...

【问题讨论】:

  • 检查呈现的代码以验证 URL。您还进行了哪些其他故障排除?
  • 查看 url 的来源时,它显示正确的 url.. 当我在浏览器的地址栏中编辑 url base_url()/index.php/Controller 时,它正在正常工作。
  • base_url() 是一个服务器端 PHP 函数,它在页面被渲染之前执行。在浏览器的地址栏中是没有意义的。
  • 是的 Sparky 我知道.. 我只是试图解释我的问题。我的地址栏当前显示 localhost/timesofeducation/ 我设置为基本 url。我的默认控制器是 Home。当我点击登录链接时,上面提到的代码创建了一个控制器登录,该 URL 显示了正确的 localhost/timesofeducation/Login。但它显示404错误。当我手动编辑 localhost/timesofeducation/index.php/Login 它的作品时。
  • 您在应用程序文件夹之外有一个 htaccess 文件吗?还要检查基本网址集。

标签: codeigniter codeigniter-3


【解决方案1】:

你必须再使用 3 个步骤:

  1. 在项目根目录(在您的项目文件夹“/project”或域根目录上)创建一个.htaccess 文件

.htaccess 文件中插入以下代码行。

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

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

  1. 找到 config/congig.php 文件并将这一行更改为-

*在我的情况下添加基本网址:

$config['base_url'] = 'http://localhost/cirest.dev/';(分配您的项目路径/URL)

$config['index_page'] = 'index.php';$config['index_page'] = '';

  1. 将此 HTML 添加到您的视图中:

&lt;li&gt;

&lt;a href="&lt;?php echo base_url('login'); ?&gt;"&gt;

&lt;i class="fa fa-unlock-alt"&gt;&lt;/i&gt;&amp;nbsp;Login &lt;/a&gt;

&lt;/li&gt;

我认为它会起作用。 :)

【讨论】:

    【解决方案2】:

    尝试用site_url() 代替base_url()

    &lt;a class="" href="&lt;?php echo site_url(); ?&gt;Login"&gt;

    【讨论】:

    • 告诉我,现在出现的是什么网址?
    • 与 base_url() 相同的 url
    【解决方案3】:

    首先你应该在config/routes.php中正确定义url:

    $route['login'] = 'login/index';
    

    然后您可以在具有 site_url() 函数的模板中使用此路由:

    <li>
      <a href="<?php echo site_url('login'); ?>"> 
      <i class="fa fa-unlock-alt"></i>&nbsp;Login </a>
    </li>
    

    【讨论】:

      【解决方案4】:

      我从 $route['login'] = ['login/index']; 改为 $route['login'] = 'login/index'; 就我而言。

      【讨论】:

        【解决方案5】:

        在 webroot 中创建一个文件并将其命名为 .htaccess,然后在其中键入此代码

        <IfModule mod_rewrite.c>
          RewriteEngine On
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
        </IfModule>
        

        在你的 application/config/config.php 文件中,找到这个

        $config ['index_page'] = '';
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-01
          • 2015-05-23
          • 2016-07-07
          相关资源
          最近更新 更多