自定义404未显示在实时服务器上的Codeigniter中

时间:2017-08-14 14:48:41

标签: codeigniter server http-status-code-404

我正在处理Codeigniter项目,如果找不到路由,我想显示自定义404页面。一切都在localhost上工作正常,但是当我在实时服务器上传我的项目时,它不再显示了。在实时服务器上它显示我默认404.并且url生成如下example.com/my404但不显示我的自定义404.请帮助我找出我遗漏的地方。谢谢你有价值的答案。

这是我的查看文件error_404.php

<!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>404 Page Not Found</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <style type="text/css">
      body
      {
        padding: 0px;
        margin:0px;
      }

      h1
      {
        padding: 0px;
        margin: 0px;
        font-size: 15px;
        text-align: center;
        font-weight: 600;
        font-family: sans-serif;
        color: #003a99;
      }

      #container
      {
        width: 100%;
        background-position: top center;
        background-repeat: no-repeat;
        background-attachment: local;
        background-size: cover;
      }

      .web
      {
        padding-top: 10px;
        padding-bottom: 85px;
      }

      span
      {
        color:#333;
      }

      .heading
      {
        color:#333;
      }
    </style>
    </head>
    <body>
        <div id="container" class="img-responsive" style="assets/images/background-image:url(404-Page-Not-Found2.jpg);">
        <img src="assets/images/404-page.png" class="img-responsive"/>
        <h1 class="heading">The page you’re looking for went fishing!</h1>
        <h1 class="web">Go back to <a href="<?php echo base_url();?>"><span>www.vpacknmove.in</span></a><h1>
        </div>
    </body>
    </html>

这是我的Controller文件My404.php

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

    public function index()
    {
        $this->output->set_status_header('404');
        $this->load->view('error_404');//loading in my template
    }
}
?>

我的路线

$route['404_override'] = 'My404';
$route['default_controller']      = 'Home';

1 个答案:

答案 0 :(得分:1)

尝试在路线中小写控制器名称:

$route['404_override'] = 'my404';

如果这不能解决您的问题,请回报。

如果您需要自定义在没有匹配路由时生成的404,该怎么办,我创建了一个文件/application/core/MY_Exception.php:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Exceptions extends CI_Exceptions {

    /**
     * Class constructor
     */
    public function __construct()
    {
        parent::__construct();
    }

    // --------------------------------------------------------------------

    /**
     * 404 Error Handler
     * -----------------
     * This method has been extended so that if we use the show_404
     * function, that it is a styled/custom 404 page that is shown.
     */
    public function show_404( $page = '', $log_error = TRUE )
    {
        if( is_cli() )
        {
            $heading = 'Not Found';
            $message = 'The controller/method pair you requested was not found.';
        }
        else
        {
            $heading = '404 Error - Page Not Found';
            $message = 'The page or resource you were looking for was not found on this server.';
        }

        // By default we log this, but allow a dev to skip it
        if( $log_error )
        {
            log_message('error', $heading . ': ' . $page );
        }

        if( is_cli() )
        {
            echo $this->show_error($heading, $message, 'error_404', 404);   
        }
        else
        {
            $CI = &get_instance();

            $CI->output->set_status_header('404');

            $view_data = [
                'heading' => $heading,
                'message' => $message
            ];

            $data = [
                'doc_title' => ['pre' => '404 Error - Page Not Found - '],
                'extra_head' => '<meta name="robots" content="noindex,nofollow" />',
                'content' => $CI->load->view( 'errors/html/error_404', $view_data, TRUE )
            ];

            $CI->load->view('templates/main', $data);

            echo $CI->output->get_output();
        }

        exit(4); // EXIT_UNKNOWN_FILE
    }

    // --------------------------------------------------------------------

}

请注意我完成它的方式,我将error_404视图嵌套在我创建的模板中。这就是我这样做的方式。您在应用程序(通常是控制器)中创建页面时使用的内容替换了该部分。