wordpress在IIS主机下URL重写实现静态化完美解决方案
2024-10-30 10:35:21 # 技术

首先需要你的网站空间服务商支持自定义错误页,然后创建一个utu-8格式的404.php文件,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
header("HTTP/1.1 200 OK");
$ori_qs = strtolower($_SERVER['QUERY_STRING']);
$pattern = '/[^;]+;[^:]+:\/\/[^\/]+(\/[^\?]*)(?:\?(.*))?/i';

preg_match($pattern, $ori_qs, $matches);
$_SERVER['PATH_INFO'] = $matches[1] . '?' . $matches[2];
$_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO'];
$query_args = explode('&', $matches[2]);
unset($_GET);
foreach ($query_args as $arg)
{
$the_arg = explode('=', $arg);
$_GET[$the_arg[0]] = $the_arg[1];
}

include('index.php');
?>

创建好之后上传至网站根目录下,然后自定义错误页为404.php页。这样就可以去后台-设置-固定链接设置而实现静态化了。但是这个方法会导致中文标签和一些相关地方的tags无法找到,解决办法是超找wp-include/classes.php中如下代码:

1
2
3
4
5
6
7
if ( isset($_SERVER['PATH_INFO']) )
$pathinfo = $_SERVER['PATH_INFO'];
else
$pathinfo = '';
$pathinfo_array = explode('?', $pathinfo);
$pathinfo = str_replace("%", "%25", $pathinfo_array[0]);
$req_uri = $_SERVER['REQUEST_URI'];

修改为:

1
2
3
4
5
6
7
if ( isset($_SERVER['PATH_INFO']) )
$pathinfo = mb_convert_encoding($_SERVER['PATH_INFO'], 'utf-8', 'GBK');
else
$pathinfo = ”;
$pathinfo_array = explode('?', $pathinfo);
$pathinfo = str_replace("%", "%25", $pathinfo_array[0]);
$req_uri = mb_convert_encoding($_SERVER['REQUEST_URI'], 'utf-8', 'GBK');