How to debug the Rewrite rule

Kato, Tetsuya/加藤 哲也 20 Reputation points
2023-12-07T05:32:03.33+00:00

I define multiple Rewrite rules on Web.config in App Service (Windows).

If you do not do the expected operation when you send a request to the App Service, is there any way to debug which rules and Precondition?

I wrote the following rules in web.config:

<rule name="RedirectFQDNChange" preCondition="condition2" enabled="true" patternSyntax="ECMAScript">
    <match serverVariable="RESPONSE_Location" pattern="^.*-etc.azurewebsites.net/(.*)" />
    <conditions>
        <add input="{HTTP_X_ORIGINAL_HOST}" pattern="site1.com" />>
    </conditions>
    <action type="Rewrite" value="https://sute1.com/{R:1}" />
</rule>




<preConditions>
    <preCondition name="condition1">
        <add input="{RESPONSE_STATUS}" pattern="3\d\d" />
    </preCondition>
    <preCondition name="condition2">
        <add input="{HTTP_X_ORIGINAL_HOST}" pattern="site1.com|site2.com" />
        <add input="{RESPONSE_STATUS}" pattern="3[0-9][0-9]" />
    </preCondition>
<preConditions>
Http_x_original_host shall be defined in site1.com. In this case, will the second Condition2 of Precondition meet and RedirectRuleChangeredirect rewrite?

If you know what the operation is defined by multiple Precondition, please let me know.
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,930 questions
{count} votes

1 answer

Sort by: Most helpful
  1. brtrach-MSFT 17,731 Reputation points Microsoft Employee Moderator
    2023-12-07T15:49:29.73+00:00

    @Kato, Tetsuya/加藤 哲也 To debug Rewrite rules in web.config, you can enable Failed Request Tracing in your App Service. This will allow you to capture detailed information about the request and response, including the execution of your Rewrite rules.

    To enable Failed Request Tracing, you can follow these steps:

    1. Open the Azure portal and navigate to your App Service.
    2. Under the "Development Tools" section, click on "Diagnostic Tools".
    3. In the Diagnostic Tools blade, click on "Enable Application Logging (Filesystem)".
    4. Set the "Level" to "Verbose".
    5. Click on "Save".
    6. In the Diagnostic Tools blade, click on "Enable Failed Request Tracing".
    7. Set the "Status Code" to "200-999".
    8. Click on "Save".

    Once Failed Request Tracing is enabled, you can reproduce the issue and then download the trace file. The trace file will contain detailed information about the request and response, including the execution of your Rewrite rules.

    Regarding your specific Rewrite rule, if the HTTP_X_ORIGINAL_HOST header matches "site1.com" and the response location header matches "-etc.azurewebsites.net", then the rule will rewrite the response location header to "https://sute1.com/{R:1}". The precondition "condition2" will only be met if the response status code is in the 300 range and the HTTP_X_ORIGINAL_HOST header matches "site1.com" or "site2.com".

    It is possible that the FRT is not configured correctly. Here are a few things you can check:

    1. First, check if the FRT is enabled for your app service. You can do this by going to the "Diagnostic settings" blade in the Azure portal and adding a diagnostic setting for your app service. Once you've done that, make sure that "Failed Request Tracing" is enabled in the diagnostic setting.
    2. Next, make sure that the FRT is configured correctly. To do this, go to the "Monitoring" blade in the Azure portal and select "Diagnostic settings" from the left-hand menu. Then, select the diagnostic setting that you created in step 1. Check that "Failed Request Tracing" is selected in the diagnostic setting.
    3. Finally, check if the FRT is capturing the correct requests. To do this, go to the "Monitoring" blade in the Azure portal and select "App Service Logs" from the left-hand menu. Then, select "Failed Request Tracing" from the top menu. Check that the requests you are trying to capture are listed in the FRT logs.

    Lastly, as a workaround and if you're looking for a quick fix, you can use Azure Application Gateway. If you’re using Azure Application Gateway, it allows you to rewrite selected content of requests and responses. You can translate URLs, modify request and response headers, and add conditions to ensure that the URL or the specified headers are rewritten only when certain conditions are met.

    If you have trouble with the above steps, please reply here so we can assist you further.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.