Simple HTTP redirect (301 or 302) using Azure Functions Proxies
Here is a basic tutorial to perform an HTTP redirect (Status code 301 or 302) using Azure Functions Proxies.
I was looking tonight for a basic, and serverless way, to perform a super simple http redirection that would redirect mydomain.com to www.mydomain.com. Pretty simple ! I decided to try Azure Functions Proxies to achieve that.
Step 1 : Create an Azure Function App
Create a simple Azure Function App:
Select “Windows” as the operation system to allow configuration of your redirection from the Portal (easier for what we want to do):
Step 2 : Create the proxy config
Open your newly created Function App and navigate to the section named “proxies”. Click the add button to create a new proxy:
{*path} means simply grab everything and inject it in a variable named “path”.
Click on the “Response override” section and fill it in like that :
Note that we are using status code 302 which performs a “temporary redirect”. But you can set it to 301 to perform a “permanent redirect”.
Note that permanent redirects are cached by web browsers and stays in cache until the cache is cleared. Use 301 wisely and prefer 302 for testing or if unsure about what you are doing.
Add a Header named “Location” and change the value to whatever URL you want. Note that we are using the “path” variable we configured in the route template. This will allow us to forward the entire path and query strings (eg: https://mydomain.com/path?query=bipbip will be redirected to https://www.mydomain.com/path?query=bipbip)
Leave the Body section empty, it is fine.
And then click the “create” button to create your proxy.
Step 3 : Test your redirection
Browse to the URL of your Azure Function App (something like https://<yourazurefunctionappname>.azurewebsites.net) and you should be redirected to https://www.mydomain.com.
Try to add a path to your url or some query arguments to your Azure Function App URL and you should see that they are kept during the redirection.
Step 4 : Configure a custom domain
You probably now want to configure a custom domain so that instead of using https://<yourazurefunctionappname>.azurewebsites.net you can leverage https://mydomain.com.
Simply go to the “Custom domains” section and click “Add a custom domain” to initiate the process.
Conclusion
I have tested many options to perform simple HTTP redirects, from setting up an Apache or Nginx, to using a AWS Application Load Balancer (ALB), but I think I love this option, because it is simple, efficient, serverless and priceless. Feel free to comment if you have a better alternative !