IIS: redirect to index when page not found

Viewed 2271

In my IIS Website i have three applications hosted on it, the main website is an Angular App, when i have API application where all APIs are hosted and another application that is a pure javascript project hosted under application alias /vmenu/.

Each request that comes to example.com/v/ should be redirected to /vmenu/ and i've yet added that rewrite rule to my web.config

<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Rewrite VMenu">
                    <match url="^v/(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$" />
                    <action type="Rewrite" url="api/{R:0}" appendQueryString="false" />
                </rule>
            </rules>
        </rewrite>/>
        </httpErrors>
    </system.webServer>
</configuration>

Then for my Angular application i have to make a rule for the following purpose :

If the app uses the Angular router, you must configure the server to return the application's host page (index.html) when asked for a file that it does not have.

The Angular documentation suggest do make the following rewrite rule:

<system.webServer>
  <rewrite>
    <rules>
      <rule name="Angular Routes" 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.html" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

But by adding it under rules in my webconfig each request is just redirected to index.html even requests to API.

So how could i make a rule to redirect only pages with 404 error to index.html?

1 Answers

You can select the error 404, and then select the “Edit” button in the Error Pages component. Choose the option that you want, If you want to redirect the error page to another URL, you can choose the third radio button and enter the URL.

enter image description here

You also can change the configuration above through Web.Config. You only need to add the value below on your <system.Webserver> tag.

<httpErrors>
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" prefixLanguageFilePath="" path="https://URL/" responseMode="Redirect" />
</httpErrors>
Related