【问题标题】:How to Redirect non-existing data page to Page Not Found in Codeignitor?如何将不存在的数据页面重定向到 Codeigniter 中未找到的页面?
【发布时间】:2021-12-03 13:04:48
【问题描述】:

当数据不存在/页面不存在时,我尝试将页面重定向到 404 Page Not Found。我可以通过路由成功将页面重定向到 404,

e.g. www.testing.com/jhdbgkj => go to 404 page is okay 

但是,我发现当 url 包含多个部分时,它无法重定向:

www.testing/product/dhjbg => cannot redirect to 404 page

我知道如果找不到传递的参数,我可以重定向到 Controller 中的 404 页面,这就是我目前正在做的事情。 (由于传递的参数用于获取一些数据,我不能只在路由中设置“产品/(:any)”以转到 404 页面)但是,我想知道是否有更快的方法来重定向到 404 页面一次,而不是在Controller中一个一个地阻塞......或者这不可能吗?

另一个问题是,是否可以将php错误页面重定向到我们自己自定义的404页面,以更好的方式显示错误页面?谢谢。

路线:

$route['(:any)'] = "common/pageNotFound";

【问题讨论】:

    标签: php codeigniter model-view-controller http-status-code-404


    【解决方案1】:

    是的,您可以通过重定向设计自己的 404 页面

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

    你的控制器就像

    <?php class My404 extends CI_Controller { public function __construct() { parent::__construct(); } public function index() { $this->output->set_status_header('404'); $this->load->view('err404');//loading in custom error view } } 
    

    【讨论】:

      猜你喜欢
      • 2017-08-30
      • 2021-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      相关资源
      最近更新 更多