I need to change domain name on aws Route 53, however I have multiple sub-domains as well. For example foo.com is my top level domain, however I also have app.foo.com and admin.foo.com.
I would like to set a single redirect for foo.com, for example to bar.com, and automatically change all sub-domains to redirect traffic to [subdomain].bar.com. So admin.foo.com would redirect to admin.bar.com. Is it possible to setup a single top level redirect that also affects all sub-domains?
2
Answers
Redirecting a domain like foo.com and all its subdomains (app.foo.com, admin.foo.com, etc.) to a new domain (bar.com) while preserving the subdomain structure can’t be done directly in Route 53 as far as I know.
But you can use CloudFront and Lambda@edge to use it as a middleware and do this redirection for you.
You just need to change domain names as CNAME pointing to the cloudfront and create the Lambda as a flexible and scalable way to handle dynamic redirection for all subdomains.
If your registrar is not Route53, you can try following:
Create a Cloudfront distribution
Create a certificate with both apex
foo.com
and wildcard subdomain*.foo.com
and attach it to your new CloudfrontCreate a new Cloudfront Function by adapting this AWS tutorial with following (not tested):
Attach the new Cloudfront Function to the Cloudfront on the viewer request on the default behavior
Create a new hosted zone in Route53
Add following entries to your Route53:
Note that both entries are required as APEX domain and subdomains are processed differently.