.netangulariisurl-rewrite-module

IIS Rewrite with angular app and application alas


here is the scenario. I have an angular app and a .net 8 web api hosting on IIS. the .net wep api is added to the site as a application with "dotnet" as the alias

I need domain.com/dotnet to pass through but everything else to get rewrite to ./index.html

when I go to domain.com/dotnet/someRequest the server just redirects to the angualr app instead of giving me the json data from the api.

this is my current IIS rewrite rule

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
             <rules>
                <rule name="Angular Routes" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{QUERY_STRING}" pattern=".*/dotnet/*" negate="true" />
                        <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>
</configuration>

Ive also tried this

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="DOTNET"  stopProcessing="true">
                    <match url=".*/dotnet/*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="UNENCODED_URL" appendQueryString="false" />
                </rule>
                <rule name="Angular Routes"  stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <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>
</configuration>

if I remove all the rewrite rules then i can get to domain.com/dotnet/getRequest and it will return the json data and i can get to the angualar app by going to domain.com.

HOWEVER if I refresh the page and my url is like domain.com/homepage then IIS cant find the page.

my goal to pass all domain.com/dotnet/* request through and everything else to get rewrite to ./index.html to avoid the refresh issue.

Any help would be greatly apricated!!


Solution

  • You could try this below rule:

     <rewrite>
                    <rule name="DotNetPassThrough" stopProcessing="true">
                        <match url="^dotnet/(.*)" />
                        <conditions>
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        </conditions>
                        <action type="None" /> 
                    </rule>
    
                    <rule name="Angular Routes" stopProcessing="true">
                        <match url=".*" />
                        <conditions>
                            <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>
    

    OR

     <rewrite>
                <rules>                   
                    <rule name="DotNetPassThrough" stopProcessing="true">
                        <match url="dotnet/(.*)" />
                        <conditions>
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        </conditions>
                        <action type="None" /> 
                    </rule>
    
                  
                    <rule name="Angular Routes" stopProcessing="true">
                        <match url=".*" />
                        <conditions>
                            <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>
    

    This rule will help you to serve the donet request as it is and will also provide an angular route.