【问题标题】:WordPress 301 Redirects on IISIIS 上的 WordPress 301 重定向
【发布时间】:2019-05-30 01:01:37
【问题描述】:

在部署新网站后,我真的很难让 301 重定向在我们的服务器上正常工作。我尝试过的一切要么导致 500 错误,要么根本无法正常工作。

以下是我的web.config 文件的重写部分摘录。

<rewrite>
    <rules>
        <rule name="wordpress" stopProcessing="true">
            <match url=".*" />
            <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            </conditions>
            <action type="Rewrite" url="index.php" />
        </rule>
        <rule name="301 Redirect 1" stopProcessing="true">
            <match url="^join$" />
            <action type="Redirect" url="careers" redirectType="Permanent" />
        </rule>
    </rules>
</rewrite>

我希望能够将 http://www.example.com/join 重定向到 http://www.example.com/careers,但我在访问 http://www.example.com/join 时却得到了 404。

我已检查并已安装并启用 URL 重写模块。

我做错了什么?

【问题讨论】:

标签: wordpress url-redirection http-status-code-301 iis-8.5


【解决方案1】:

将您的 301 重定向作为第一条规则移到 wordpress 规则之前。

<rewrite>
    <rules>
        <rule name="301 Redirect 1" stopProcessing="true">
            <match url="^join$" />
            <action type="Redirect" url="careers" redirectType="Permanent" />
        </rule>
        <rule name="wordpress" stopProcessing="true">
            <match url=".*" />
            <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            </conditions>
            <action type="Rewrite" url="index.php" />
        </rule>
    </rules>
</rewrite>

【讨论】:

  • 这似乎有效,您能解释一下原因吗?
  • 你的 ".*" 也匹配 join,并且 stopProcessing 在第一次尝试中停止规则匹配,然后你总是在旧设置中命中第一条规则。
【解决方案2】:

替换 web.config 文件中的代码并使用以下代码更新它

 <?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

【讨论】:

  • 此答案未能解决主要问题。事实上,这是一个倒退,因为它忽略了包含失败的重定向。