Expert

Coen Adrien van Driel

I'm a software architect and software quality & testing enthusiast. My focus is on using Microsoft technologies from C# to Azure to create awesome products.

I'm also the founder of ExpertSwitch, a software consulting company.

Forwarding Domain Names with ASP.NET Core and Azure Web Apps

January 14, 2019

A common issue when registering domain names is the question whether you should purchase multiple TLD’s (top-level domains) to accommodate for spelling variations, or preemptively purchase domains for future projects to prevent competitors to get ahead.

Considering the relatively low price of most domain names and the continuously shrinking list of freely available domains, most organizations follow the ‘better safe than sorry’-policy when it comes to registering additional domains. This often results in organizations registering an array of domain names, that have no actual use besides forwarding to the main domain or functioning as a parked domain for future projects.

All the registered additional domain name variants preferably forward to your main domain to redirect visitors into the right direction. Some domain name registrars provide this forwarding-feature as a (often paid) service.

In this article I will explain how you can create your own forwarding-service using a simple ASP.NET Core web application, hosted on Azure Web Apps

Creating A Domain Forwarding Application

The domain forwarding application essentially only needs one class, Startup.cs. The code works by simply processing all incoming requests and checking if the origin hostname matches with any domains that are registered in our domain forwarding list.

The Configure-method checks for a match in our domain register and adds a redirect url/domain to the response, alternatively, if no domain matches, an HTTP error code will be returned.

If you have to manage a large list of domains, it may be wise to convert the domain register from a dictionary to a dynamic data source (e.g. database) so you will not have to alter the code and publish the app on every change.

Note: Make sure to add both naked domains, and www-domains to the domain register if applicable.

Full demo project for this article can be found on GitHub: DomainForwardingApp

    public class Startup
    {
        private static IDictionary<string, string> Domains { get; set; }

        public Startup()
        {
            RegisterDomains();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.Run(context =>
            {
                string originDomain = context.Request.Host.Value;
                bool isRegisteredDomain = TryGetTargetDomain(originDomain, out string targetDomain);
                if (isRegisteredDomain)
                {
                    context.Response.Redirect(targetDomain, permanent: true);
                }
                else
                {
                    context.Response.StatusCode = 404;
                }
                return Task.FromResult<object>(null);
            });
        }

        private static bool TryGetTargetDomain(string originDomain, out string targetDomain)
        {
            bool isRegisteredDomain;
            if (Domains.ContainsKey(originDomain))
            {
                targetDomain = Domains[originDomain];
                isRegisteredDomain = true;
            }
            else
            {
                targetDomain = null;
                isRegisteredDomain = false;
            }

            return isRegisteredDomain;
        }

        private static void RegisterDomains()
        {
            const string mainDomain = "https://www.maindomain.com";

            //Key: origin domain, Value: target domain
            Domains = new Dictionary<string, string>();
            Domains.Add("otherdomain.com", mainDomain);
            Domains.Add("www.otherdomain.com", mainDomain);
        }
    }

Setting Up Your Azure Web App and DNS Records

With the application ready to go, all we need now is a place to host it. You can add the application to your IIS web server and be up and running (don't forget to add the correct bindings for all forwarded domains), or you can host the application on Azure Web Apps. Especially if you are already running a App Service Plan it will be cost efficient to run it it as an Azure Web App.

Creating the Azure Web App

For this application to run, all you need to do is create a simple Azure Web App with default settings. Make sure that the App Service Plan you select enables you to configure custom domains.

azure custom domains

Consult the Azure documentation on how to create an Azure Web App if this step is unclear.

After your Web App instance has been deployed successfully you can publish the application to Azure directly from Visual Studio by creating a Publish Profile, or by uploading the files manually to the Azure Portal.

Configuring Your Custom Domains and DNS Records

Configuring custom domains is a basic feature of Azure Web Apps and you can consult the Azure documentation on how to set up your domains. You will need to add DNS records at your domain registrar's control panel to have your domains point to your Azure Web App. Make sure to add a CNAME-record for WWW if you also want to forward www.yourdomain.com. Set your TTL (Time-To-Live) to the lowest timeframe for your changes to propagate quickly.

azure custom domains dns records

As the final step, configure your Azure Web App with your custom domains so it will accept and bind the traffic coming from your origin domain to your application to be forwarded.

azure custom hostnames

Full demo project for this article can be found on GitHub: DomainForwardingApp