【问题标题】:How can I redirect 404 page to home page in codeigniter如何在codeigniter中将404页面重定向到主页
【发布时间】:2021-08-09 07:46:51
【问题描述】:

如果有人在正确的 url 之后键入内容然后用户重定向到主页,我想将所有 404 错误自定义页面重定向到 codeigniter 中的主页。

【问题讨论】:

标签: .htaccess codeigniter redirect


【解决方案1】:

首先,进入 config -> routes,然后向下滚动直到找到

$route['404_override'] = 'error404'; //by default it's '' (empty)

其次,你创建一个控制器Error404.php

<?php
class Error404 extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        redirect('home'); //your controller (like Home.php or any controller that you want to call)
    }
}

【讨论】:

  • 另外,如果你问我为什么要创建新控制器而不是把 home 控制器放在 override 中:你可以直接从 404_overide 调用 'home' 控制器,它也会引导你回家。但是地址栏不会更新(例如,如果您输入 www.yourdomain.com/xxxxxx/xxxxx/xxxx,如果您直接从 404_overide 调用 'home',您将被定向到家,但地址栏保持 www.yourdomain。 com/xxxxxx/xxxxxx/xxxx,而不是 www.yourdomain.com/home。
【解决方案2】:

<?php
class Error extends CI_Controller
{
  function __construct()
  {
    parent::__construct();
  }
  function index(){
    $this->load->view('home');
  }
}
?>

【讨论】:

    【解决方案3】:

    编辑error_404.php文件(FilePath: application/views/errors/html/error_404.php)

    &lt;/body&gt; 标签之前添加您的代码,如下所示:

    <?php
    header("Location: $yourhomepageurl");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      • 2017-08-30
      • 2015-03-25
      • 2013-01-31
      相关资源
      最近更新 更多