Search code examples
angulariisurl-rewritingvirtual-directory

IIS Rewrite 2 Angular applications


I have 2 Angular applications. One in my main domain and the other one in a virtual directory called "merchant", inside this domain. I want to have rewrite rules for both so that I can enter the URL in the address bar without having 404 page. For this I put a web.config in both folders, but I am not able to get both applications rewrite rules working at the same time.

In the main application web.config I have

<rewrite>
      <rules>
        <rule name="block" stopProcessing="true">
            <match url="^merchant/(.*)$" />
            <action type="Redirect" url="merchant/{R:1}" />
          </rule>
       <rule name="Application Routes" patternSyntax="Wildcard">
          <match url="*" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />

          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>

With this conf, the rewriting works well for the main application, but not for the second. I thought the first rule would redirect to the virtual folder web.config but seems it's not. Below the rules in my virtual folder web.config:

<rewrite>
      <rules>
        <rule name="AngularJS 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="/merchant/" />
        </rule>
      </rules>
    </rewrite>

Of course if I remove all the rules from the main web.config, the second is working, but not the first anymore...

How can I get both at the same time?


Solution

  • Finally I found a solution. As commented by funkizer I had to put all rules in the same web.config Below the main web.config with 2 rules

    <rewrite>
      <rules>
       <rule name="AngularJS Routes" stopProcessing="true">
          <match url="^merchant/(.*)$" />
          <conditions >
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="merchant/" />
        </rule>
       <rule name="Application Routes" patternSyntax="Wildcard">
          <match url="*" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
    

    I also had to keep the other web.config but I removed all rules inside. Now both applications can be accessed with direct url