Search code examples
url-rewritingweb-configazure-web-app-serviceweb-config-transform

What is the proper way to add rewrite rules in an Azure App Service


I want to add re-write rules to my Azure app service, but if I even try create a web.config with a simple rewrite as shown below, the site returns an HTTP 500.

<?xml version="1.0" encoding="UTF-8"?>

<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Test" stopProcessing="true">
                    <match url="test" />
                    <action type="Rewrite" url="/index.html" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

What I did is create the web.config from scratch and FTP it into the wwwroot folder for the app service which is where I Ftp'd the rest of my web site files.

I have seen an old conversation about web.config files in Azure, but I'm not even sure if this is in regards to Azure app service - and things change so quickly in Azure that I thought I should ask separately anyways.

I have also seen this link about using web.config transforms. Maybe this is required?

This other link seems to indicate that updating web.config is possible, but their example results in an HTTP 500 for me as well.

I've looked through configuration options in the Azure portal for my app service and nothing seems to jump out at me.


Solution

  • >>”This other link seems to indicate that updating web.config is possible”

    Yes, we could update the configuration of Web App by updating Web.config file directly. You could upload a new version of configuration file to wwwroot folder by FTP/WebDeply or any other ways provided by Kudu. Another easy way to edit files in wwwroot folder is using the App Service Editor, you could use it to edit the configuration file.

    I can’t reproduce your problem on my side. Following are what I done.

    1. Create a Web App using a Web App Plan with “Shared” SKU.
    2. Create 2 file, a index.html file and a Web.config file. The content in Web.config is same as yours.
    3. Upload these files to wwwroot folder of my web app using FTP.
    4. Open a web browser and input a URL like this “http: // xxx.azurewebsites.net/test”. The web server will return the content of index.html. It proves that the rewrite is working fine.

    >>”but their example results in an HTTP 500 for me as well.”

    Firstly, please mark sure whether the rewrite is working fine or not. I suggest you view the index.html page directly and check whether the error code(500) comes from index.html page.

    Sometimes we provide the wrong syntax for rewrite rule, it will return 500 (URL Rewrite Module Error). Have you enabled Detailed Error Logging and Web Server Logging for your web app. If yes, you could view the detail error message to get more useful information. For how to enable and view diagnostics logging, link below is for your reference.

    Enable diagnostics logging for web apps in Azure App Service