Codeigniter 错误 url 重定向 404

Codeigniter wrong url to redirect 404

我想在输入错误url时显示404页面

真URL

https://localhost.com/posts/hello_world

假 URL

https://localhost.com/posts/dsadasd

错误 url 到 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);
}

试试这个

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);
}

如果真正理解您的问题是什么,此函数会根据您通过 url 获得的 slug 在没有帖子时显示 404 页面。