Search code examples
iisurl-rewritingjettyiis-10

How do I return 404 response if no URL Rewrite Rules are matched


I'm running IIS 10 on Windows 2019. I have the following two rules

        <rules>
             <rule name="Reverse Proxy Inbound Rule 1" stopProcessing="true">
                 <match url="ServiceName1(.*)" ignoreCase="false" />
                 <action type="Rewrite" url="http://127.0.0.1:9090/{R:1}" />
             </rule>
             <rule name="Reverse Proxy Inbound Rule 2" stopProcessing="true">
                 <match url="ServiceName2(.*)" ignoreCase="false" />
                 <action type="Rewrite" url="http://127.0.0.1:9091/{R:1}" />
             </rule>
         </rules>
     </rewrite>

Basically, if neither of these rules are matched, am I able to return a 404 response. At the minute I'm running Jetty and it is returning

502 - Web server received an invalid response while acting as a gateway or proxy server.


Solution

  • You could use below rule to set custom page using url rewrite:

     <rules>
                <rule name="test" enabled="true" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="CustomResponse" statusCode="404" subStatusCode="4" statusReason="this is custom error page" statusDescription="test error" />
                </rule>
    

    or

    <rule name="Handle404" stopProcessing="true">
                    <match url="(.*)" />
                   
                    <action type="Rewrite" url="/path/to/404.html" />
                </rule>
    

    and set below code in web.conifg file under <system.webServer> section:

     <httpErrors errorMode="Custom" existingResponse="Replace" />