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

225 字
2 分钟
...阅读
...评论

这个问题是因为 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)根据具体情况做相应调整。

搞定!

评论区
Copyright © Bean Deng