Create a web 301 redirect service with Microsoft Azure Functions

I ran into a nice issue, and I suppose more people could run into the same issue. Lets first start with laying out the issue:

  • We are moving to a new web solution where only one domain (pepperbyte.com) is supported unless you move all of the domains to the new web provider (which costs us 15% per domain extra).
  • The new web solution provider does not support htaccess or 301 redirects from other domains (as an example pepperbyte.nl) then the root domain (pepperbyte.com).
  • Our DNS provider can do a lot but they cannot add URL redirections or Aliases.
  • We don’t have a spare web server somewhere where we can add an htaccess file and I’m sure not want to pay for it 🙂
  • There are “free” services on the web which allows you to redirect your domains to the root domain, but these free services will display adds or you need to add a link to the redirect service.

Ok, you can have your remarks on above point, but hey that’s just the way it is 🙂

You can read more about 301 redirects and what it means in this article.

So, I was thinking how could I solve this thing in a robust and future-proof solution, and came up with a Microsoft Azure Function! Nice, but what’s an Azure Function?

According to the Microsoft Docs:

Azure Functions is a solution for easily running small pieces of code, or “functions,” in the cloud. You can write just the code you need for the problem at hand, without worrying about a whole application or the infrastructure to run it. Functions can make development even more productive, and you can use your development language of choice, such as C#, F#, Node.js, Java, or PHP. Pay only for the time your code runs and trust Azure to scale as needed. Azure Functions lets you develop serverless applications on Microsoft Azure.

So let’s begin!

Step 1: Create your Azure Function App

The first step is to create our Function App, press the Create a resource – search for Function App and press the following resource: Function App here I am

Create your Function App

Next, you need to give the Function App a name, choose your subscription, Resource Group etc. After you have pressed Create the Function App will be created for you.

Step 2: Create your Function

In the next step we must create the function, just go to the Functions under your Function App name press the New function button.

Add a new function here            Choose your trigger

Choose the HTTP trigger because we want to trigger the function when the page is visited. Choose C#, give the function a Name and choose the default authorization level.

Create your new Azure Function HTTP Trigger

Step 3: The code

The next step is to change the code. Just replace the code you see in the function by the following code snippet:


using static System.Environment;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    string OriginUrl = req.Headers.GetValues("DISGUISED-HOST").FirstOrDefault(); 
    log.Info("RequestURI org: " + OriginUrl);
  
    //create response
    var response = req.CreateResponse(HttpStatusCode.MovedPermanently);

    if((OriginUrl.Contains("pepperbyte")) || 
       (OriginUrl.Contains("yourdomainhere"))) 
    { 
           response.Headers.Location = new Uri("http://www.pepperbyte.com");
    } 
    else
    {
       return req.CreateResponse(HttpStatusCode.InternalServerError);
    }

    return response;
}

I think this code is pretty much self-explanatory. There is one Header I use which needs more explanation – DISGUISED-HOST – : when we finish up our Function App we will retrieve a URL in order of https://redirectus.azurewebsites.net/api/TriggerMe?code=3INMBFiRmoFX206OGeMQUBNpaZn4taZRx9pzUn1e1h2t4heFoZ6aPg==

When you browse to the root domain https://redirectus.azurewebsites.net you will not be redirected to the HTTP Trigger Function we just created, in order to do so we need to create a Proxy, the proxy will route the traffic from the root to our HTTP trigger, but the RequestURI will be that of our root domain (https://redirectus.azurewebsites.net) instead of the domain which needs to be redirected (i.e. https://www.pepperbyte.nl). The redirected domain (pepperbyte.nl) is the one which will be presented in the DISGUISED-HOST header.

Step 4: Proxy

In this step, we will add the Proxy, click on the + sign to create a new proxy. Give the proxy a name, add {*path} to the Route template and in the Backend URL add the HTTP Trigger URL of our Function App which you can find in the Function Get Function URL. Press the Create and all of the traffic will be rerouted to the HTTP Trigger Function App.

Create the Proxy Step 5: add your Custom domain

The final step is to actually add your domain which needs to be redirected, in our case pepperbyte.nl. Start by clicking the main application name, in our case redirectus. Click on the Platform features and press Custom domains.

Overview and Platform features of your Azure Function App

Press the Add hostname.Add your own host here

Add your domain which needs to be redirected, with the * as we want all of the subdomains to be redirected too. In our case, it’s *.pepperbyte.nl. As you can see you have to add a CNAME in your DNS entries, well you can do that now. Create a CNAME record with the name *, content redirectus.azurewebsites.net, and a TTL of 1 hour.

Add your CNAME at your DNS registrar and validate

Press Validate until the domain can be validated!
Validation complete

Step 6: test

The proof is in the pudding: if we open our browser and type http://this-is-a-test.pepperbyte.nl it should bring us to https://www.pepperbyte.com. And yes it’s working 🙂

Errors or debugging?

In case it’s not working for you, just check out the Monitoring item within you Function App, you should see something according to below screenshot. You can add as much logging as you wish.

Monitoring log

Well, that wraps things up for now!

Daniel Nikolic

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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