之前分享过一篇文章《WP中去掉固定链接中的index.php和category》,其中写到关于win主机WordPress实现伪静态的方法中的一种是利用404页面来做的,因为那时候数据量还不是很大,所以就没有自己研究,这今天突然发现google管理工具的中发现了很多奇怪的错误页面,仔细看了博客之后才发现,存在很多错误链接,这些错误链接的都是文
章列表分页的链接。
现在我的文章列表有4页,正常情况从博客首页观看的时候,你随意点击任何列表比如:点击第四页出来的地址是
http://www.andy87.net/news/,这个是正常的,但是当时继续点击其他分页的时候比如第2页出来的地址就是
http://www.andy87.net/404.php/page/2?404;http://www.andy87.net/404.php/page/2,继续点击第3页出来的地址是
http://www.andy87.net/404.php/page/3?404;http://www.andy87.net/404.php/page/3很奇怪,还有其他分类列表分页也是这样的。
在网上了找了一下,原来我的那个404.php的代码已经过时了,最后经过终于找到了一个比较完美的404代码。注意:404.php修改为你的404页面名称即可,如下:
<?php // This is the default file for the site. Usually index.php $default = 'index.php'; // The name of this file. // Set this value for the URL in Custom Error Properties of your website in IIS. // Goto: IIS Manager > Websites > [Site Name] > Properties > Custom Errors > // 404 & 404;2 & 404;3 > URL (Requires a '/' prefix in IIS). $thisfile = '404.php'; //404.php修改为你的404页面名称即可 $_SERVER['ORIG_PATH_TRANSLATED'] = str_replace($thisfile, $default, $_SERVER['ORIG_PATH_TRANSLATED']); $_SERVER['SCRIPT_FILENAME'] = str_replace($thisfile, $default, $_SERVER['SCRIPT_FILENAME']); $_SERVER['ORIG_PATH_INFO'] = str_replace($thisfile, $default, $_SERVER['ORIG_PATH_INFO']); $_SERVER['SCRIPT_NAME'] = str_replace($thisfile, $default, $_SERVER['SCRIPT_NAME']); $_SERVER['PHP_SELF'] = str_replace($thisfile, $default, $_SERVER['PHP_SELF']); $_SERVER['PATH_INFO'] = false; $qs =& $_SERVER['QUERY_STRING']; $ru =& $_SERVER['REQUEST_URI']; $pos = strrpos($qs, '://'); $pos = strpos($qs, '/', $pos + 4); $_SERVER['URL'] = $ru = substr($qs, $pos); $qs = trim(stristr($ru, '?'), '?'); // Required for WordPress 2.8+ $_SERVER['HTTP_X_ORIGINAL_URL'] = $ru; // Fix GET vars foreach ( $_GET as $var => $val ) { if ( substr($var, 0, 3) == '404') { if ( strstr($var, '?') ) { $newvar = substr($var, strpos($var, '?') + 1); $_GET[$newvar] = $val; } unset($_GET[$var]); } break; } include($default); ?>
在此对之前的404代码进行了修正,以前的那个代码过时了。。。。
遇到了类似的问题呢
想请教, 按这做法, 当没有纪录时 (真正的404状态), 页面会全空白, 那怎加404讯息在上面? 留空不太好…
<?php
// This is the default file for the site. Usually index.php
$default = ‘index.php’;
// The name of this file.
// Set this value for the URL in Custom Error Properties of your website in IIS.
// Goto: IIS Manager > Websites > [Site Name] > Properties > Custom Errors >
// 404 & 404;2 & 404;3 > URL (Requires a ‘/’ prefix in IIS).
$thisfile = ‘404.php’; //404.php修改为你的404页面名称即可
$_SERVER[‘ORIG_PATH_TRANSLATED’] = str_replace($thisfile, $default, $_SERVER[‘ORIG_PATH_TRANSLATED’]);
$_SERVER[‘SCRIPT_FILENAME’] = str_replace($thisfile, $default, $_SERVER[‘SCRIPT_FILENAME’]);
$_SERVER[‘ORIG_PATH_INFO’] = str_replace($thisfile, $default, $_SERVER[‘ORIG_PATH_INFO’]);
$_SERVER[‘SCRIPT_NAME’] = str_replace($thisfile, $default, $_SERVER[‘SCRIPT_NAME’]);
$_SERVER[‘PHP_SELF’] = str_replace($thisfile, $default, $_SERVER[‘PHP_SELF’]);
$_SERVER[‘PATH_INFO’] = false;
$qs =& $_SERVER[‘QUERY_STRING’];
$ru =& $_SERVER[‘REQUEST_URI’];
$pos = strrpos($qs, ‘://’);
$pos = strpos($qs, ‘/’, $pos + 4);
$_SERVER[‘URL’] = $ru = substr($qs, $pos);
$qs = trim(stristr($ru, ‘?’), ‘?’);
// Required for WordPress 2.8+
$_SERVER[‘HTTP_X_ORIGINAL_URL’] = $ru;
// Fix GET vars
foreach ( $_GET as $var => $val ) {
if ( substr($var, 0, 3) == ‘404’) {
if ( strstr($var, ‘?’) ) {
$newvar = substr($var, strpos($var, ‘?’) + 1);
$_GET[$newvar] = $val;
}
unset($_GET[$var]);
}
break;
}
include($default);
?>
再次测试 ❓
谢谢,很有帮助….