Azure web App is ignoring web.config URL rewrite

58 Views Asked by At

I have an azure web app. It is deployed as a framework dependant app targeting net8.0 and the runtime target is portable.

I have written a web.config which is in the wwwroot directory.

I am trying to use a url rewrite to redirect the default domain sitename.azurewebsites.net to my actual website https://sitename.com I dont want the default site accessible.

The web app seems to ignore my web.config no matter what I try. The web.config contents are as follows:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <location path="." inheritInChildApplications="false">
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="Redirect rquests to default azure websites domain" stopProcessing="true">
                        <match url="(.*)" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="^sitename\.azurewebsites\.net$" />
                        </conditions>
                        <action type="Redirect" url="https://sitename.com.au/{R:0}" redirectType="Permanent" />
                    </rule>
                </rules>
            </rewrite>
            <handlers>
                <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
            </handlers>
            <aspNetCore processPath="dotnet" arguments=".\sitename.dll" stdoutLogEnabled="false" stdoutLogFile="\\?\%home%\LogFiles\stdout" hostingModel="inprocess" />
        </system.webServer>
    </location>
</configuration>

Can anyone point out what I am doing wrong?

Ive tried changing the publishing the web.config to the server, i can confirm it is in the site root. Azure is just ignoring it.

1

There are 1 best solutions below

0
Sirra Sneha On

The web app seems to ignore my web.config no matter what I try

It ignores because we need to place web.config file in the root directory of the project, rather than keeping it in the wwwroot folders. When we publish ASP.NET Core applications, it creates a web.config file by default in home/site/wwwroots in KUDU.

enter image description here

In the below code I used https://www.google.com as Redirect URL.

web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <location path="." inheritInChildApplications="false">
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="Redirect requests to default Azure Websites domain" stopProcessing="true">
                        <match url="(.*)" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="^urlrewritingapp2024\.azurewebsites\.net$" />
                        </conditions>
                        <action type="Redirect" url="https://www.google.com/" redirectType="Permanent" />
                    </rule>
                </rules>
            </rewrite>
            <handlers>
                <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
            </handlers>
            <aspNetCore processPath="dotnet" arguments=".\sitename.dll" stdoutLogEnabled="false" stdoutLogFile="\\?\%home%\LogFiles\stdout" hostingModel="inprocess" />
        </system.webServer>
    </location>
</configuration>

Here's the web.config file after the deployment.

enter image description here

Here's the successful output:

My actual URL is https://urltest12024.azurewebsites.net and its Redirected to Google.com

enter image description here