URL redirection using Azure App Service

A colleague had setup something along these lines at work. It’s a good idea and I couldn’t figure out any blog post or docs mentioning such a use case, so thought I’d write something up.

What we wanted to do was have URL redirection. That’s to say if someone visits https://rakhesh.com I want it to forward to https://rakhesh.net. Simple stuff, and I think the recently announced Azure Front Door can be used for the same now. Of course when he setup redirection this wasn’t available so he had to resort to other means. What he did was setup an App Service and modify its web.config file to do IIS redirects. While I knew of App Services and knew you could modify the web.config setting via Application Settings I hadn’t realized you can entirely replace it.

In fact, before jumping into web.config I should probably first mention that IIS has a URL Rewrite extension and while that is something you usually install manually it is present by default in App Service. Here’s an example web.config that redirects all requests to Google:

So what one needs to do to setup a redirection service is create an App Service and somehow replace its web.config with one similar to the above that does the redirection for you. The App Service should use IIS so we’ll go with one of the .NET ones.

It’s probably easiest to create an App Service via the Portal so feel free to do it that way. Below is a rough sketch of the steps via CLI:

Replace all the $ variables accordingly of course.

One thing I did above, which you’ll have to do manually if creating this from the portal, is to set the deployment type as local Git. Again, it doesn’t have to be local Git – you can use any other deployment method to push the web.config, or you can totally skip this and edit the web.config file manually (I’ll show how in a bit). But that’s what the --deployment-local-git switch in the last command above stands for.

In my case I ended up with an App Service with the following URL: https://rak-362143-wa.azurewebsites.net. Let’s say the domain I want to actually redirect is az.rakhesh.com so what I’ll do now is 1) create a CNAME entry in DNS pointing az.rakhesh.com to rak-362143-wa.azurewebsites.net, and 2) add az.rakhesh.com as a custom domain to the App Service. Again, you can do the latter via the portal but here’s how to do it via the CLI:

Assuming no errors, we are good to proceed.

(Just to add: at this point only http://az.rakhesh.com will work. To get https working I’ll have to add a cert – skipping that for now).

Now onto the web.config.

I’ll go into how to modify the web.config file directly on the App Service – i.e. not via Git like I setup above. For this we can make use of something called Kudu. This is the engine that runs on an App Service and does the actual building of your code when you push it to an App Service. Turns out for every App Service like https://rak-362143-wa.azurewebsites.net you can go to a URL like https://rak-362143-wa.scm.azurewebsites.net that takes you to its Kudu page. On this page if you click on Debug Console > PowerShell > and in the file manager that opens if you click on the site > wwwroot folder you can then click the + icon to create a new web.config file.

Then click on the pencil icon next to the file, paste the web.config text I had above and Save it. And that’s it! At this point if I go to http://az.rakhesh.com it will redirect me to Google. :)

Instead of creating and editing the file via Kudu it is also possible to create a file locally and push via Git. This is why I enabled local Git deployment above. What this does is that the App Service now has a Git repo associated to it with a username password combo, and one can push to this Git repo to make any changes to the App Service.

If you want to try this delete the web.config file created above and read on…

The quickest way to see the Git repo details in the Portal is to go to the Deployment Center and see Local Git/FTPS credentials. Or to get these via the portal you can do the following:

After this change to some directory and clone $gitrepo. Enter the password when prompted:

Next change to the folder that was created and create a folder called FolderToDeploy in that (I didn’t do this initially and figured I had to do this from the error messages). Create web.config in here with the same info as earlier. And do a git add . and git commit -m "initial commit" and git push as usual. In my case it threw the following error :)

This bummed me for a while until I realized that locally my Git repo branch was called main while in Azure it was master. So I made a new branch locally called master, set the upstream, and pushed again. This time it worked! (Note: if you are following along, don’t do this yet. Read on for one more issue I encountered, fix it, and then push).


Unfortunately this too does not work. :D What happens is the web.config has been copied to a folder called FolderToDeploy but that’s of no use to us as we want it to be in the root of wwwroot. The trick to get it to copy over as I want is in this article. What I have to do is set the following in the Application Settings of the app:

I can do that via the CLI as follows:

Now I can push the config again. This time you’ll see it removes the FolderToDeploy after the code is pushed:

Now if I go to http://az.rakhesh.com/ it will redirect as expected!