IIS 上的 WordPress 301 重定向

WordPress 301 Redirects on IIS

部署新网站后,我真的很难让 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 to http://www.example.com/careers but I just get a 404 while accessing http://www.example.com/join

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

我做错了什么?

将您的 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>

替换您的 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>