WordPress利用404.php实现Windows主机下伪静态

香港Windows主机 (627) 2015-11-26 14:44:09

操作方法:

1、进入WP后台,【设置】--【固定连接】里面,先把固定连接选择自定义,设置成/%post_id%.html。

2、在桌面新建一个txt文本文档,然后名字重命名改成404.php。再用DW软件打开404.php把下面的代码复制粘贴进去,保存退出。

3、上传下面的404.php页面到网站根目录下,然后在虚拟主机控制面板里面修改404跳转页面为刚才上传的404.php,这样就可以实现伪静态了。

代码如下:

<?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-handler.php';

$_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);

?>

THE END