使用 _remap() 函数。这使您可以执行一些非常强大的操作,其中包含超出正常内置范围的404错误 - 例如
- 不同的404消息取决于用户是否登录!
- 额外记录 404 错误 - 您现在可以看到 404 的引荐人是什么,从而帮助追踪错误。这包括查看它是否是机器人(例如Google机器人) - 或者是否是登录用户,哪个用户导致了404(因此您可以与他们联系以获取更多信息 - 或者如果他们试图猜测管理员路由或其他内容,请禁止他们)
- 忽略某些 404 错误(例如 iPhone 用户的预合成.png错误)。
- 如果您有特定需求,允许某些控制器处理自己的404错误(例如,允许博客控制器重新路由到最新的博客)
让所有控制器扩展 :MY_Controller
class MY_Controller extends CI_Controller
{
    // Remap the 404 error functions
    public function _remap($method, $params = array())
    {
        // Check if the requested route exists
        if (method_exists($this, $method))
        {
            // Method exists - so just continue as normal
            return call_user_func_array(array($this, $method), $params);
        }
        //*** If you reach here you have a 404 error - do whatever you want! ***//
        // Set status header to a 404 for SEO
        $this->output->set_status_header('404');
        // ignore 404 errors for -precomposed.png errors to save my logs and
        // stop baby jesus crying
        if ( ! (strpos($_SERVER['REQUEST_URI'], '-precomposed.png')))
        {
            // This custom 404 log error records as much information as possible
            // about the 404. This gives us alot of information to help fix it in
            // the future. You can change this as required for your needs   
            log_message('error', '404:  ***METHOD: '.var_export($method, TRUE).'  ***PARAMS: '.var_export($params, TRUE).'  ***SERVER: '.var_export($_SERVER, TRUE).' ***SESSION: '.var_export($this->session->all_userdata(), TRUE));
        }
        // Check if user is logged in or not
        if ($this->ion_auth->logged_in())
        {
            // Show 404 page for logged in users
            $this->load->view('404_logged_in');
        }
        else
        {
            // Show 404 page for people not logged in
            $this->load->view('404_normal');
        }
   }
然后,将您的404设置为主控制器,设置为不存在的功能 - 即routes.php
$route['404']             = "welcome/my_404";
$route['404_override']    = 'welcome/my_404';
但是欢迎中没有函数 - 这意味着您的所有404都将通过该函数 - 因此您实现了DRY并将所有404逻辑放在一个地方。my_404()_remap
这取决于你,如果你使用或只是在你的逻辑中。如果使用 - 则只需修改 Exceptions 类即可重定向show_404()redirect('my_404')show_404()
class MY_Exceptions extends CI_Exceptions
{
    function show_404($page = '', $log_error = TRUE)
    {
         redirect('my_404');
    }
}