How do I redirect a domain in CloudFront?

Last updated: 2022-05-27

I want to redirect my domain to another domain in Amazon CloudFront. How can I do this?

Short description

To redirect a domain in CloudFront, use one of the following:

  • An Amazon Simple Storage Service (Amazon S3) website endpoint that returns a 301 status code
  • An edge function that redirects requests to the new domain

An Amazon S3 website endpoint redirection moves the existing URL path to the redirected domain. Edge functions allow you to redirect viewers to other pages based on information in the request, or rewrite all requests from one path to another.

Resolution

Use an Amazon S3 endpoint that returns a 301 status code

Follow the instructions in the Redirecting to a different URI path or from HTTP to HTTPS using Amazon S3 and CloudFront section of How can I redirect one domain to another in Route 53?

Use an edge function that redirects requests to the new domain

For more information on the difference between Lambda@Edge and CloudFront functions, see Choosing between CloudFront Functions and Lambda@Edge.

For Lambda@Edge:

See Customizing at the edge with Lambda@Edge. For example code, see Example: Generating an HTTP redirect (generated response).

For CloudFront Functions: 

See Customizing at the edge with CloudFront Functions. For example code, see Redirect the viewer to a new URL.