Search code examples
azureazure-devopsazure-functionsazure-web-app-serviceweb-config

Azure redirecting app service to custom domain


I am trying to redirect the default domain to a custom domain using web.config file in root directory of app service. In my case, https://default.azurewebsites.net to https://app.customdomain.com which is working but https://default.azurewebsites.net/login is not redirecting to https://app.customdomain.com my working config file

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<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>
          <rule name="Redirect rquests to default azure websites domain" stopProcessing="true">
            <match url="(.*)" />  
            <conditions logicalGrouping="MatchAny">
              <add input="{HTTP_HOST}" pattern="^default\.azurewebsites\.net$" />
            </conditions>
            <action type="Redirect" url="https://app.customdomain.com/{R:0}"  />  
          </rule>
    </rules>
  </rewrite>
</system.webServer>
</configuration>

I also tried adding two rules it didn't work

    <rule name="Redirect rquests to default azure websites domain for login" stopProcessing="true">
            <match url="(.*)/login" />  
            <conditions logicalGrouping="MatchAny">
              <add input="{HTTP_HOST}" pattern="^default\.azurewebsites\.net$" />
            </conditions>
            <action type="Redirect" url="https://app.customdomain.com/login/{R:0}"  />  
          </rule>
    <rule name="Redirect rquests to default azure websites domain" stopProcessing="true">
            <match url="(.*)" />  
            <conditions logicalGrouping="MatchAny">
              <add input="{HTTP_HOST}" pattern="^default\.azurewebsites\.net$" />
            </conditions>
            <action type="Redirect" url="https://app.customdomain.com/{R:0}"  />  
          </rule>

Also tried one rule with multiple conditions. didn't work

<rule name="Redirect rquests to default azure websites domain" stopProcessing="true">
            <match url="(.*)" />  
            <conditions logicalGrouping="MatchAny">
              <add input="{HTTP_HOST}" pattern="^default\.azurewebsites\.net$" />
              <add input="{HTTP_HOST}" pattern="^default\.azurewebsites\.net/login" />
            </conditions>
            <action type="Redirect" url="https://https://app.customdomain.com/{R:0}"  />  
          </rule>

few more combinations with regex also tried but still no luck.


Solution

  • <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="Redirect default azure domain to custom domain" stopProcessing="true">
                        <match url="(.*)" />
                        <conditions logicalGrouping="MatchAny">
                            <add input="{HTTP_HOST}" pattern="^testing\.azurewebsites\.net" />
                        </conditions>
                        <action type="Redirect" url="https://grew.mydomain.com/login"  />
                    </rule>
                    
                    <rule name="Redirect default azure domain login url to custom domain" stopProcessing="true">
                        <match url="(.*)^login" />
                        <conditions logicalGrouping="MatchAll">
                            <add input="{HTTP_HOST}" pattern="^testing\.azurewebsites\.net" />
                            <add input="{PATH_INFO}" pattern="^/login" />
                        </conditions>
                        <action type="Redirect" url="https://grew.mydomain.com/login"  />
                    </rule>
                    
                    <rule name="Angular rule" 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>
    </configuration>
    

    Finally the above web.config file worked!!! Redirecting rule should be defined above the angular rule and rule name should be unique