I have written an external redirect file for use in a site. The redirect works perfectly, but when I add a 404 rule, the redirect breaks. At that point, 404 works, but the redirect does not. I’ve also tried keeping the redirect rules inside of the web.config file (instead of as a separate file), but this does not work either.
Can anyone figure out how I can get my redirects and 404 rules working in tandem?
Below is the code for the redirect in the web.config file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rewriteMaps configSource="rewritemaps.config" />
<rules>
<rule name="Redirect rule1 for Redirects">
<match url=".*" />
<conditions>
<add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The external file referenced above looks like this:
<rewriteMaps>
<rewriteMap name="Redirects">
<add key="/old_page.html" value="/new_page.html" />
</rewriteMap>
</rewriteMaps>
At this point everything works great. However, as soon as I add the following, the 404 works great, but the redirect does not work.
<httpErrors>
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/page-not-found.html" responseMode="ExecuteURL" />
</httpErrors>
The total code that does not work is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rewriteMaps configSource="rewritemaps.config" />
<rules>
<rule name="Redirect rule1 for Redirects">
<match url=".*" />
<conditions>
<add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
<httpErrors>
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/page-not-found.html" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
</configuration>
Your config is fine — no issues here.
Try recycling app pool .. or restart IIS service.