Create a HTTP redirect app in Azure

Background

During the past couple of weeks I’ve been working with a customer to move a system to Azure. The system uses multiple web front-ends and as part of the move they will be assigned to new domains. With some of the web front-ends being used by external users I wanted to make sure that those still could access the applications after the move, ideally without having any IIS-servers to maintain.

The solution

After doing some research I found that this should be easy to accomplish using a Web App in Azure App Service.

I created the new Web App and app service plan. The name wasn’t really important as a custom domain would be used. After creating the web app I went ahead and added the custom domain (verifying DNS etc.) to the web app. I also uploaded a certificate as some of the URLs pointing to the service would have used HTTPS.

If you’re planning on using this solution for redirecting internal traffic, note that only publicly trusted certificates are supported in Azure Web Apps.

With the new App Service Editor (currently in preview) I didn’t have to bother with FTP or other publishing tools to upload my code.

All I had to do was create the “web.config” file in wwwroot and paste my code, it’s saved automatically and instantly ready to test.

Azure App Service Editor

The code I used was similar (if not identical) to the code I’ve previously used to accomplish this on IIS. I wanted the redirection to replace the domain part of the URL without affecting the remaining URL for example:

  • https://oldapp.technut.com/ -> https://newapp.technut.se/
  • https://oldapp.technut.com/login -> https://newapp.technut.se/login

I ended up with the following code:

<configuration>
    <system.webServer>
        <rewrite>
   <rules>
      <rule name="oldapp.technut.com" enabled="true">
         <match url="^(.*)$" />
         <conditions>
            <add input="{HTTP_HOST}" pattern="^oldapp\.technut\.com$" />
         </conditions>
         <action type="Redirect" url="https://newapp.technut.se/{R:1}" />
      </rule>
   </rules>
</rewrite>
    </system.webServer>
</configuration>

If you want to redirect multiple URLs using the same app all you need to do is add the domains and repeat rows 5-11 before “</rules>”

Leave a Reply

Your email address will not be published. Required fields are marked *