Setting up URL redirection and rewrite rules in Azure Front Door
TypeScriptURL redirection and rewrite rules are an essential part of web infrastructure management, allowing you to control the flow of traffic and the structure of URLs in a way that improves user experience, consolidates domains, or meets other operational requirements.
In Azure Front Door, redirection can be used to send users from one URL to another, which is useful for situations such as moving traffic from HTTP to HTTPS, redirecting from a www subdomain to a non-www, or redirecting from an old domain to a new one.
Rewrite rules, on the other hand, allow you to modify the incoming request URL or header before forwarding the request to the backend pool without the user seeing the changes. This can be used to standardize URLs, direct to specific backend pools based on the URL path, or modify headers for backend compatibility or tracking purposes.
Below is a Pulumi program written in TypeScript that sets up an Azure Front Door with URL redirection and rewrite rules. The program demonstrates how to create:
- A Front Door instance
- A Frontend endpoint
- A Backend pool
- Routing rules that include a redirection action
- Routing rules that include a URL rewrite action
Please remember that you need to have Pulumi installed and configured with Azure credentials to use this code, and you should replace placeholder values with your specific settings.
import * as pulumi from "@pulumi/pulumi"; import * as azureNative from "@pulumi/azure-native"; const resourceGroup = new azureNative.resources.ResourceGroup("resourceGroup", { resourceGroupName: "my-resource-group", location: "West US", }); const frontDoor = new azureNative.network.FrontDoor("frontDoor", { resourceGroupName: resourceGroup.name, location: "Global", enabledState: "Enabled", friendlyName: "myFrontDoor", frontendEndpoints: [ { name: "myFrontendEndpoint", hostName: "example-FrontDoor.azurefd.net", // replace with your Azure Front Door hostname }, ], backendPools: [ { name: "myBackendPool", backends: [ { address: "example-backend.azurewebsites.net", // replace with your backend address httpPort: 80, httpsPort: 443, weight: 1, priority: 1, }, ], }, ], routingRules: [ { name: "redirectRule", frontendEndpoints: [ { // Associate with frontend endpoint defined above id: pulumi.interpolate`${frontDoor.id}/frontendEndpoints/myFrontendEndpoint`, }, ], routeConfiguration: { redirectConfiguration: { redirectType: "Found", // choose from "Moved", "Found", "TemporaryRedirect", "PermanentRedirect" redirectProtocol: "HttpsOnly", customHost: "www.example-redirect.com", // replace with your redirect destination host }, }, }, { name: "rewriteRule", frontendEndpoints: [ { // Associate with frontend endpoint defined above id: pulumi.interpolate`${frontDoor.id}/frontendEndpoints/myFrontendEndpoint`, }, ], routeConfiguration: { urlRewrite: { path: "/newpath", // New path for URL rewrite queryString: "", }, forwardingConfiguration: { backendPool: { // Associate with backend pool defined above id: pulumi.interpolate`${frontDoor.id}/backendPools/myBackendPool`, }, }, }, }, ], }); export const frontDoorName = frontDoor.name; export const frontDoorUrl = pulumi.interpolate`https://${frontDoor.frontendEndpoints[0].hostName}`;
In the above code:
- We create a new resource group where our Front Door service will live.
- We then define an instance of
azureNative.network.FrontDoor
, configuring its properties to enable the service, name it, and specify its location as 'Global' (as Azure Front Door is a global service). - We added a frontend endpoint to our Front Door instance; each endpoint represents a URL that users can visit.
- We set up a backend pool, which is a set of backend services that will serve the actual content. The backend in this example could be an Azure Web App or any other supported backend service.
- We create two routing rules:
- The
redirectRule
will match requests on the Frontend endpoint and issue a redirect (HTTP 302) to the specified hostname with HTTPS as the protocol. - The
rewriteRule
will rewrite the incoming request path to/newpath
before forwarding it to the backend pool.
- The
Lastly, we export the Front Door name and URL so you can easily reference and access it after deployment.
Remember to replace the placeholders, such as the backend address, hostname, and other specific settings, with your own information. The routing rules will take effect as soon as the Front Door service is provisioned and deployed by Pulumi.