- Newest
- Most votes
- Most comments
In general, I see two options to solve this:
- Use multiple domains on one CloudFront distribution
- Define one domain as the "main domain", and redirect the other to this one
Using multiple domains on one distribution requires the setup of CNAMEs, you can find a short HOWTO here.
The second option will return an HTTP 301 whenever a request on the secondary domain is recorded. You can do this on S3, or with an Edge function (which is probably easier to achieve). You can find details about this solution here.
In general, I recommend not setting up two distributions. As you have mentioned, it has a cost impact, but more importantly it will double your maintenance work - which is not optimal.
The links provided assume that you are hosting a website, which then makes use of the dynamic API. In that case, redirecting works smoothly. If you have an API only, redirecting will be more difficult since RESTful APIs in general don't support this type of redirects. For API only setups, I recommend configuring the target domain directly within your application (please note that you might need CORS settings in this case, i.e. when your application is served from one domain and uses an API endpoint on another domain).
Relevant content
- asked 2 years ago
- asked 3 years ago
- AWS OFFICIALUpdated 8 days ago
- AWS OFFICIALUpdated 9 months ago
- AWS OFFICIALUpdated 4 months ago
- AWS OFFICIALUpdated 3 years ago
I have already done the multiple domains on a single distribution. this of course works but ends up as 2 separate domains which is not ideal. I have tried an edge function in CloudFront, but it it does not allow URL rewrite.
I am just looking for an obvious piece of documentation that explains this, I find it strange that AWS does not consider this a scenario and have it as part of the setup process. Support agents offered different advice to you, they said I need 2 CloudFronts, which is actually what it looks like based on the limitations of the techstack.
If you follow the link provided by Michael above to set up a single distribution to cover both domain names, you could then use a CloudFront Function to redirect requests based on the host header. eg. if the host header includes www, send a 301 response to redirect them to the domain apex. Make sure to append the URI to the redirect. There's an example here that you could use as a starting point: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/example-function-redirect-url.html
Exactly, thank you Paul. Rewriting the URL in the request does not solve the issue here, because the client would still use the old / original URL in subsequent requests. You want to tell the correct domain to the client, not to the origin.
If a request arrives on the www-domain, use the edge function to directly send a response without passing the request to the CloudFront origin. The HTTP 301 instructs the browser to send the request to the non-www domain.
If you haven't hard-coded the target domain in the application, the browser should then use the non-www domain for all subsequent requests.