简体   繁体   中英

How to create custom 404page

I want to create custom 404 page for my site.I mean when i type url as

http://example.com/anydummytext

need to redirect to custom 404page

How to create custom page for this

The simple way is create a controller you can name the error controller to any what you want.

Example

<?php

class Not_found extends CI_Controller {

   public function index() {
       // some content

       $this->load->view('not_found');
   }

}

Then on another controller you can redirect it

redirect('not_found');

Example Only

<?php

 class Home extends CI_Controller {

   public function index() {

       $result = $this->some_model->get();

       if ($result) {
          // content

          $this->load->view('home');

       } else {
          redirect('not_found');
       }

   }
 }

The other option is in config/routes.php you can use codeigniter

$route['404_override'] = 'not_found'; // Note will not work in a subfolder

You can create a new controller or use existing controller to write down a function for loading the VIEW for 404 .

class Nopage extends CI_Controller
{
    /* Index function
     *
     * return void
     */
    public function index()
    {
            $this->load->view('nopage_404');
        }
    }
}

Call the controller function in config->routes as below.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM