# Hosting a permanent redirect in Azure

https://jaredrhodes.com/blog/hosting-a-permanent-redirect-in-azure/

WordPress charges for each domain attached to a blog. That is a bit much when you consider .net, .org, .whatever domains that are used on top of the .com domain most use. To get the use out of a single domain, a permanent redirect can be used. Since Azure has a fixed price, invariant of how many domains you host there; it can be used for the permanent redirect.

## Prerequisites

- An [Azure App Service](https://docs.microsoft.com/en-us/azure/app-service/) app on a paid tier (**Shared** or better - Free/Shared plans cannot map custom domains).
- A domain you own with access to its DNS records at the registrar.

## Map the domain

In your registrar's DNS settings, create a **CNAME** record pointing `www` (or the subdomain you want) at `<your-app>.azurewebsites.net`. For the apex domain, use the **A record** Azure shows under the app's *Custom domains* blade instead (CNAME at the apex is not supported by most registrars).

Then in the portal: **App Service > Custom domains > Add hostname**, validate, and let Azure wire up the mapping.

## Add the redirect

Open the Kudu console for the app (`https://<your-app>.scm.azurewebsites.net`), navigate to `site -> wwwroot`, and edit (or create) `web.config`:

```xml
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Redirect old-domain to new-domain" stopProcessing="true">
                    <match url=".*" />
                    <action type="Redirect" url="http://<<appdomain>>/{R:0}" redirectType="Permanent" />
                </rule>              
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
```

Replace `<<appdomain>>` with the domain you wish to redirect to. The `{R:0}` preserves the original path, so deep links like `/2018/some-post/` land on the same path at the new domain - which is exactly what a permanent (`301`) redirect should do for search engines that are still holding onto the old URLs.
