彻底解决WP中文标签404错误

找到网站个目录下文件夹:wp-include文件夹下的class-wp.php,定位此代码段(V3.6在144行)

老高温馨提示:使用本教程前请备份数据库及相关文件

代码语言:javascript
复制
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'];

修改为

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");

至此,wordpress已经学会读中文了。

还有一种解决方式,即给每一个标签都设置一个英文别名,这样设置的标签还是不能使用中文,可以使用下面的代码将所有的标签格式化:

代码语言:javascript
复制
<?php
header("Content-type: text/html; charset=utf-8");
// 修改:LAOGAO https://blog.phpgao.com/
define('MYSQL_HOSTNAME', 'localhost');
define('MYSQL_USERNAME', 'yourusername');
define('MYSQL_PASSWORD', 'yourpassword');
define('MYSQL_DATABASE', 'yourdatabase');
define('MYSQL_TABLEPRE', 'wp
'); // 表前缀

error_reporting(E_ALL);
ini_set(&#39;display_errors&#39;, &#39;On&#39;);

$link = mysql_connect(MYSQL_HOSTNAME, MYSQL_USERNAME, MYSQL_PASSWORD);
if (!$link) {
    die(&#39;Could not connect: &#39; . mysql_error());
}
echo &#39;Connected successfully&lt;br /&gt;&#39;;

mysql_select_db(MYSQL_DATABASE, $link);

// 下面三句的作用是设置当前连接编码为UTF-8标准。
// 所以请确保你的WordPress数据库是符合UTF-8编码标准,
// 否则请自行将下面的UTF-8改成相应的字符集。
mysql_set_charset(&#39;utf8&#39;, $link);
mysql_query(&#39;SET NAMES UTF8&#39;);
mysql_query(&#34;SET character_set_results = &#39;utf8&#39;, &#34; . &#34;character_set_client = &#39;utf8&#39;, &#34; . &#34;character_set_connection = &#39;utf8&#39;, &#34; . &#34;character_set_database = &#39;utf8&#39;, &#34; . &#34;character_set_server = &#39;utf8&#39;&#34;, $link);
// 字符设置结束
echo &#39;&lt;br /&gt;&#39;;

// 下面为encode编码tag中的中文slug
$res = mysql_query(&#34;SELECT `term_id`,`name` FROM &#34; . MYSQL_TABLEPRE . &#34;terms&#34;);
//echo &#34;SELECT `term_id`,`slug` FROM &#34;.MYSQL_TABLEPRE.&#34;terms&#34;;
while ($row = mysql_fetch_array($res)) {
    echo &#39;id=&#39; . $row[0] . &#39; - &#39; . &#39;name=&#39; . $row[1] . &#39; --- &#39;;
    if (mysql_query(&#39;UPDATE `&#39; . MYSQL_TABLEPRE . &#39;terms` SET `slug`=\&#39;&#39; . urlencode($row[1]) . &#39;\&#39; WHERE `term_id` =&#39; . $row[0])) {
        echo &#39;UPDATED!&lt;br /&gt;&#39;;
    }
}
// 编码结束

mysql_close($link);

echo &#39;完成!&lt;br /&gt;&#39;;
exit();

?>