zhaoJian's Tech Notes

WordPress URL Rewrite for Static URLs on IIS Host - Perfect Solution

Technology ~1515 words · 4 min read - views

First, your web hosting provider needs to support custom error pages. Then create a 404.php file in UTF-8 format with the following code:

<?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');
?>

After creating it, upload to the website root directory, then set the custom error page to 404.php. This way you can go to Dashboard - Settings - Permalinks to enable static URLs. However, this method causes Chinese tags and some related tags to not be found. The solution is to find the following code in wp-include/classes.php:

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'];

Change to:

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');
Share:

Comments