为啥/在找到页面时 CodeIgniter 在哪里设置 404 http 状态?
Posted
技术标签:
【中文标题】为啥/在找到页面时 CodeIgniter 在哪里设置 404 http 状态?【英文标题】:Why/Where does CodeIgniter set a 404 http status when the page is found?为什么/在找到页面时 CodeIgniter 在哪里设置 404 http 状态? 【发布时间】:2016-08-05 07:16:05 【问题描述】:我正在使用 CodeIgniter2。我正在使用路由将 url 段路由到控制器和方法。
这似乎有效。我的页面按预期加载,即 url 使用正确的方法从数据库中获取页面信息,然后返回并显示正确的页面。我的routes.php相关代码是:
$route['default_controller'] = "content";
$route['en/(:num)/(:any)'] = "content/en/$1";
$route['de/(:num)/(:any)'] = "content/de/$1";
$route['es/(:num)/(:any)'] = "content/es/$1";
$route['it/(:num)/(:any)'] = "content/it/$1";
$route['ar/(:num)/(:any)'] = "content/ar/$1";
$route['404_override'] = '';
但是,显示的页面不是显示 http 状态 200,而是显示 http 状态 404 ...我不知道为什么。
我怀疑这与 MY_Router.php 文件有关,我必须提供自定义错误页面,但我无法弄清楚发生了什么。
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Router extends CI_Router
var $error_controller = 'error';
var $error_method_404 = 'error_404';
function My_Router()
parent::CI_Router();
// this is just the same method as in Router.php, with show_404() replaced by $this->error_404();
function _validate_request($segments)
// Does the requested controller exist in the root folder?
if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
return $segments;
// Is the controller in a sub-folder?
if (is_dir(APPPATH.'controllers/'.$segments[0]))
// Set the directory and remove it from the segment array
$this->set_directory($segments[0]);
$segments = array_slice($segments, 1);
if (count($segments) > 0)
// Does the requested controller exist in the sub-folder?
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
return $this->error_404();
else
$this->set_class($this->default_controller);
$this->set_method('index');
// Does the default controller exist in the sub-folder?
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
$this->directory = '';
return array();
return $segments;
// Can't find the requested controller...
return $this->error_404();
function error_404()
$this->directory = "";
$segments = array();
$segments[] = $this->error_controller;
$segments[] = $this->error_method_404;
return $segments;
function fetch_class()
// if method doesn't exist in class, change
// class to error and method to error_404
$this->check_method();
return $this->class;
function check_method()
$ignore_remap = true;
$class = $this->class;
if (class_exists($class))
// methods for this class
$class_methods = array_map('strtolower', get_class_methods($class));
// ignore controllers using _remap()
if($ignore_remap && in_array('_remap', $class_methods))
return;
if (! in_array(strtolower($this->method), $class_methods))
$this->directory = "";
$this->class = $this->error_controller;
$this->method = $this->error_method_404;
include(APPPATH.'controllers/'.$this->fetch_directory().$this->error_controller.EXT);
function show_404()
include(APPPATH.'controllers/'.$this->fetch_directory().$this->error_controller.EXT);
call_user_func(array($this->error_controller, $this->error_method_404));
/* End of file MY_Router.php */
/* Location: ./system/application/libraries/MY_Router.php */
【问题讨论】:
不确定是否相关,但MY_Router
的代码属于CI
谢谢Tpojka - 你怎么知道的,你知道我在哪里可以获得升级版吗?可能会工作,可能不会!谢谢
因为语法。几年前,属性是用var
声明的。您可以通过可见性声明省略它(检查here)。第二件事是构造函数。应该是public function __construct() parent::__construct();
。 PHP docs。将其更改为开始。
【参考方案1】:
已解决 - 网站中集成的 wordpress 博客正在为所有非 wordpress 页面(即 codeigniter 页面)设置 404 状态
CI的index.php中有如下代码需要注释掉
/*
*---------------------------------------------------------------
* WORDPRESS INTEGRATION
*---------------------------------------------------------------
* The ci_site_url function helps to avoid collision between WP & CI.
*/
//header("HTTP/1.0 200 OK");
define('WP_USE_THEMES', false);
require_once './blog/wp-blog-header.php';
add_filter('site_url', 'ci_site_url', 1);
function ci_site_url()
include(APPPATH.'/config/config.php');
return $config['base_url'];
【讨论】:
以上是关于为啥/在找到页面时 CodeIgniter 在哪里设置 404 http 状态?的主要内容,如果未能解决你的问题,请参考以下文章
为啥我在 codeigniter 中的分页不起作用? (404 未找到)
为啥 codeIgniter 显示在此服务器上未找到请求的 URL?
只有主页显示所有其他页面在 xampp codeigniter 中显示错误 404