/ 技术

IIS下WordPress中文URL无法访问的解决方法

这个问题是因为IIS和wordpress对url的编码不一致的问题,导致传到wordpress里的URL中的中文是乱码,所以wordpress无法给出正确的页面。解决方法很简单:

在网站根目录下建立一个php文件,名字自定,例如chineseUrl.php,内容如下:

<?php
    if (isset($_SERVER['HTTP_X_ORIGINAL_URL'])) {
        // IIS Mod-Rewrite
        $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];
    }
    else if (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
        // IIS Isapi_Rewrite
        $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL'];
    }
    else {
        // Use ORIG_PATH_INFO if there is no PATH_INFO
        (!isset($_SERVER['PATH_INFO']) && isset($_SERVER['ORIG_PATH_INFO'])) && ($_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO']);
        // Some IIS + PHP configurations puts the script-name in the path-info (No need to append it twice)
        if (isset($_SERVER['PATH_INFO'])) {
                ($_SERVER['PATH_INFO'] == $_SERVER['SCRIPT_NAME']) ? ($_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO']) : ($_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO']);
        }
        // Append the query string if it exists and isn't null
        (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) && ($_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING']);
    }
    require("index.php");
?>

修改根目录下的web.config文件,在rules节点里添加如下节点:

<rule name="chineseUrl" stopProcessing="true">
    <match url="^(tag|category)/(.*)$" />
    <action type="Rewrite" url="chineseurl.php" />
</rule>

这里的(tag|category)根据具体情况做相应调整。

搞定!

IIS下WordPress中文URL无法访问的解决方法
分享