Codeigniter错误的网址重定向404

时间:2017-11-25 23:40:20

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

我想在输入错误的网址时显示404页面

TRUE URL

https://localhost.com/posts/hello_world

错误的网址

https://localhost.com/posts/dsadasd

虚假网址到404

控制器

public function read($slug_posts)
{
    $options    = $this->options_model->listing();

    $posts      = $this->posts_model->read($slug_posts);
    #$listing   = $this->posts_model->home();

    $data = array(  'title'         => $posts->post_title.' – '.$options->option_title,
                    'description'   => $posts->post_description,
                    'posts'         => $posts,
                    #'listing'      => $listing,
                    'content'           =>  'posts/read'
                    );

    $this->load->view('layout/wrapper', $data, FALSE);
}

1 个答案:

答案 0 :(得分:0)

试试这个

public function read($slug_posts)
{
    $options    = $this->options_model->listing();

     $posts      = $this->posts_model->read($slug_posts);
     if ( ! $posts) 
     {
         show_404();
     }
     #$listing   = $this->posts_model->home();

     $data = array(  'title'         => $posts->post_title.' – '.$options->option_title,
                     'description'   => $posts->post_description,
                     'posts'         => $posts,
                     #'listing'      => $listing,
                     'content'           =>  'posts/read'
                     );

    $this->load->view('layout/wrapper', $data, FALSE);
}

如果真正了解您的问题是什么,当根据您通过网址获取的slug没有帖子时,此功能会显示404页面。