Search code examples
azure-web-app-serviceweb-config

Azure App service: Redirect all domains to the same .com Domain


I am hosting a website in Azure App service.
I registered many domains to point all to the same IP Address:

example.com  
example.ch  
example.de  
example.asia  
...  

I want that all Domains will point to the same url:

www.example.com

So if a user navigates to example.net, the browser should send him to www.example.com.

I tried to do this with adding a rule to my webconfig:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
        <httpRuntime maxQueryStringLength="32768" maxUrlLength="65536"/>
    </system.web>
    <system.webServer>
        <security>
            <requestFiltering>
                <requestLimits maxQueryString="32768"/>
            </requestFiltering>
        </security>
        <rewrite>
            <rules>
                <rule name="RedirectNonWwwToWww" stopProcessing="true">
                  <match url="(.*)" />
                  <conditions>
                    <add input="{HTTP_HOST}" pattern="^example.(.*)$" />
                  </conditions>
                  <action type="Redirect" url="http://www.example.com/{R:0}" redirectType="Permanent" />
                </rule>
               
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

This works only half.
If I navigate to example.com, it navigates to www.example.com.
But if I navigate to example.net, it navigates to www.example.net.
Is my pattern ^example.(.*)$ valid?

Strange. Can you tell me the best approach to do this? Is the webconfig even a good start? Or are there any settings in the app service itself for such a configuration?


Solution

  • I can answer the question myself.
    This is the solution web.config for my requirements:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <system.web>
            <httpRuntime maxQueryStringLength="32768" maxUrlLength="65536"/>
        </system.web>
        <system.webServer>
            <security>
                <requestFiltering>
                    <requestLimits maxQueryString="32768"/>
                </requestFiltering>
            </security>
            <rewrite>
                <rules>
                    <rule name="Redirect to www.xxx.com">
                      <match url=".*" />
                          <conditions>
                            <add input="{HTTP_HOST}" pattern="^(example\.com|example\.net|example\.asia|example\.cn|example\.com\.cn|example\.net\.cn|example\.de|example\.hk|example\.co\.in|example\.in|example\.com\.sg|example\.com\.tw|example\.tw)$" />
                          </conditions>
    
                      <action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" />
                    </rule>              
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>