skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. If your registrar is not Route53, you can try following:

    1. Create a Cloudfront distribution

    2. Create a certificate with both apex foo.com and wildcard subdomain *.foo.com and attach it to your new Cloudfront

    3. Create a new Cloudfront Function by adapting this AWS tutorial with following (not tested):

      async function handler(event) {
          const request = event.request;
          const host = request.headers.host.value;
          const path = request.uri;
          const newHost = host.replace("foo.com", "bar.com"); // Will replace just the host and will keep subdomain
          var newurl = `https://${newHost} + path`;
          var response = {
              statusCode: 302,
              statusDescription: 'Found',
              headers: { "location": { "value": newurl } }
          }
          return response;
      }
      
    4. Attach the new Cloudfront Function to the Cloudfront on the viewer request on the default behavior

    5. Create a new hosted zone in Route53

    6. Add following entries to your Route53:

      • foo.com A alias record pointing to the Cloudfront URL
      • *.foo.com A alias record pointing to the Cloudfront URL

    Note that both entries are required as APEX domain and subdomains are processed differently.

    1. Copy the NS entries of your new Route53 hosted zone
    2. Go to your registrar and update the NS entries of your foo.com domain with the NS entries from your new Route53 hosted zone
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search