Rewirte turns to redirect in app service virtual application

Shaun Chen 21 Reputation points
2024-05-22T10:53:33.1166667+00:00

Step to reproduce:

  1. /site/wwwroot/web.config:
       <?xml version="1.0"?>
       <configuration>
           <system.webServer>
               <rewrite>
                   <rules>
                       <rule name="SPA 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="/" />
                       </rule>
                   </rules>
               </rewrite>
           </system.webServer>
       </configuration>
    
    which effectively serves /site/wwwroot/index.html for all requests with unmatched path. i.e. a user will either get 200(uncached) or 304(cached) response from the app service.
    1. delete /site/wwwroot/web.config else the virtual application will not work(dunno why, but I'm fine with it because I don't have a "root" application to serve)
    2. in virtual application and directory add virtual application with /test as path and \site\test as the physical path.
    3. /site/test/web.config:
            <?xml version="1.0"?>
            <configuration>
                <system.webServer>
                    <rewrite>
                        <rules>
                            <rule name="SPA 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="/test" />
                            </rule>
                        </rules>
                    </rewrite>
                </system.webServer>
            </configuration>
      
  2. visit https://<appservcie>/test the virtual app seems to be working fine. But when I visit https://<appservcie>/test/anypath the app service actually returns a 301 redirect response to /test, unlike the behavior in 1.. And this behavior stops user from directly accessing one of those sub-routes in a single page application by forcing a user to start from the entry route(i.e. /test)

I could not find any documentation describing this behavior nor could I find a way to stop it.

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,408 questions
0 comments No comments
{count} votes

Accepted answer
  1. ajkuma 24,971 Reputation points Microsoft Employee
    2024-05-27T09:58:26.8533333+00:00

    Sharing the solution shared by @Shaun Chen with the community. Thanks for confirming and sharing the solution.

    Scenario: WebApp hosted on an App Service. The application is configured to serve index.html for all requests with unmatched paths, either returning a 200 (uncached) or 304 (cached) response. Virtual application with /test as the path and \site\test as the physical path.

    Issue: When visiting the URL https://<appservice>/test/anypath, the App Service returns a 301 redirect response to /test. This behavior is different from the root application and prevents users from directly accessing sub-routes in a single-page application, forcing them to start from the entry route (i.e., /test).

    Things Tried:

    1. You have configured the web.config in the root directory (/site/wwwroot/web.config) to serve index.html for all requests with unmatched paths.
    2. You deleted /site/wwwroot/web.config because the virtual application would not work otherwise.
    3. You added a virtual application with /test as the path and \site\test as the physical path in the virtual application and directory.
    4. You configured the web.config in the test directory (/site/test/web.config) to rewrite URLs to /test.

    Answer shared by @Shaun Chen.

    Resolution :

    Modifying/add the trailing slash rewrite URL.

    Referenced URLs:

    Note: This response contains a reference to a third-party World Wide Web site. Microsoft is providing this information as a convenience to you.


     If the answer helped (pointed you in the right direction) > please click Accept Answer - it will benefit the community to find the answers quickly.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Shaun Chen 21 Reputation points
    2024-05-25T15:53:29.6833333+00:00