Settings in web.config are ignored for URL rewrite

MHS 0 Reputation points
2024-03-21T07:09:26.1833333+00:00

I have a web app running on azure as a framework-dependent net8.0 app, on windows. I want to use a URL rewrite so that the default azure domain is redirected to my actual site. I have a web.config which is below. A version of what is below worked for a while but stopped and I have tried several iterations all of which seem to be ignored. Is there something I need to do in order to have the URL rewrite implemented in the web.config or is there a better way of doing this. I have tried restarting the web app. It does not change the behaviour and the wep app continues to ignore the contents of web.config.

Per you suggestion my web.config contains the code below. I have tried restarting the web app, it has not changed the behaviour, the web app still ignores the content of web.config.

Azure App Configuration
Azure App Configuration
An Azure service that provides hosted, universal storage for Azure app configurations.
207 questions
Azure Static Web Apps
Azure Static Web Apps
An Azure service that provides streamlined full-stack web app development.
762 questions
{count} votes

2 answers

Sort by: Most helpful
  1. brtrach-MSFT 15,251 Reputation points Microsoft Employee
    2024-03-26T02:42:30.8133333+00:00

    @MHS The template that we have in our notes is (to be placed in a web.config file):

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.webServer>   
        <rewrite>
            <rules>
                <rule name="Test" stopProcessing="true">
                    <match url=".*" />
                    <action type="Rewrite" url=https://www.contoso.com />
               </rule>
            </rules>
        </rewrite>
      </system.webServer>
    </configuration>
    
    
    

    Can you please verify if you are using Windows or Linux?

    Otherwise, can you please ensure that your web.config file is placed in the root directory of your web app?


  2. brtrach-MSFT 15,251 Reputation points Microsoft Employee
    2024-03-28T03:10:14.9733333+00:00

    @MHS I have spent the last 3 hours trying to review your site via internal and external tooling. I believe everything is properly configured in regard to the custom domain and SSL certificate.I can also see the reboot you preformed on your web app yesterday to get the changes to apply.

    The last item that I think is worth trying is to call out the order that you have the sections of your web.config file might be improper and causing the issue.

    The order of elements in a web.config file can matter, especially when it comes to the <system.webServer> section, which contains configuration settings for the IIS web server.

    In the case of the <handlers> and URL rewrite rules within the <rewrite> section, the typical convention is to place the <rewrite> section before the <handlers> section. This is because the URL rewrite module operates at an earlier stage in the request processing pipeline than the module responsible for handling the request (which in the case of ASP.NET Core is the AspNetCoreModuleV2).

    Here's the general order you might see in a web.config file:

    <configuration>
      ...
      <system.webServer>
        ...
        <rewrite>
          <rules>
            <!-- URL rewrite rules here -->
          </rules>
        </rewrite>
        <handlers>
          <!-- Handler definitions here -->
          <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
        </handlers>
        <aspNetCore ... />
        ...
      </system.webServer>
      ...
    </configuration>
    

    By placing the <rewrite> section before the <handlers> section, you ensure that the URL is rewritten before it reaches the handler that will process the request. If the order were reversed, the handler might process the original URL before it gets rewritten, which could lead to unexpected behavior.

    It's also important to note that the <aspNetCore> section is typically placed after the <handlers> section because it contains settings that are specific to the ASP.NET Core module referenced in the <handlers> section.

    Can you please try reordering your web.config file sections, restart your web app, and try again? (Keep in mind browser cache may even play a role so you might want to wait a few minutes after the site reboot and try to hit the internal domain name via inPrivate/inCognito window).

    Please let us know the outcome. If not, we have a final step we can try.

    0 comments No comments