4

I am really struggling with getting the 301 redirects working on our server after deploying a new website. Everything I have tried has either resulted in a 500 error or just plain not working.

Below is the rewrite section excerpt from my web.config file.

<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>

I was expecting to be able to redirect http://www.example.com/join to http://www.example.com/careers but I just get a 404 while accessing http://www.example.com/join.

I have checked and the URL Rewrite module is installed and enabled.

What am I doing wrong?

Burgi
  • 421
  • 8
  • 24
  • [Using Failed Request Tracing to Trace Rewrite Rules | Microsoft Docs](https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules) – DavidPostill Jan 03 '19 at 12:38
  • [Testing Rewrite Rule Patterns | Microsoft Docs](https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/testing-rewrite-rule-patterns) – DavidPostill Jan 03 '19 at 12:41

2 Answers2

2

move your 301 redirect as first rule before the wordpress rule.

<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>
Allen
  • 6,505
  • 16
  • 19
  • This appears to be working, are you able to explain why? – Burgi Jan 09 '19 at 10:33
  • 1
    your ".*" matches join too, and the stopProcessing stopped rule match in the first try, then you always hit the first rule in your old setting. – Allen Jan 09 '19 at 12:58
-1

Replace the code in your web.config file and update it with below code

 <?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>
Pratik bhatt
  • 488
  • 8
  • 23
  • This answer fails to address the primary issue. In fact it is a step backwards as it neglects to include the failing redirect. – Burgi Jan 14 '19 at 09:00