Redirect a hosted domain to another is failing

0

Aim:

To redirect all requests from bricks.dev.savvy.site to butterflies-child-care.click

What works:

  1. Create a bucket and switch on Redirect in Properties in the Static website hosting section. Supply the host: butterflies-child-care.click

  2. In cloudfront: alternate domain: bricks.dev.savvy.site with origin the S3 bucket created in step 1

  3. Follow https://d22t818ujc6ya6.cloudfront.net and it correctly redirects

    curl -IL https://d22t818ujc6ya6.cloudfront.net 301 Moved Permanently Location: https://butterflies-child-care.click/index.html

Also, curl -IL bricks.dev.savvy.site Correctly redirects as above

What doesn't work:

Instead of a 'redirect-bucket' I want to create an object inside a bucket. This object will specify the redirection. Reason: theoretically I may have hundreds of redirect entries from different url's.

  1. Create a bucket with static website hosting: redirect.dev.savvy.site (Alternatively I tried with no static website hosting but this did not make a difference)

  2. Create an object inside of this bucket with metadata to redirect curl -IL https://s3.eu-west-1.amazonaws.com/redirects.dev.savvy.site/bricks.dev.savvy.site x-amz-website-redirect-location: https://butterflies-child-care.click

  3. Point the cloudfront entry to this object curl -IL https://d22t818ujc6ya6.cloudfront.net 403 Error

Because the link to the S3 object (see step 5 above) is correct, I'm assuming that my cloudfront setup (in step 6) is incorrect. Can you please help?

Rishad
asked 7 months ago198 views
1 Answer
0

I would not do this with an object in a bucket. You can do this far more efficiently with a Lambda@Edge Function in a CloudFront distribution. To do this you would use a single CloudFront distribution. You can configure by default up 100 alternate domain names fo r a CloudFront distribtion.

Place a Lambda@Edge function on your distribution that redirects based on the host-header that it receives.

As an example:

function handler(event) {
    var request = event.request;
    var requestHeaders = request.headers;
    console.log("Request: " + JSON.stringify(request) + ".");

    // The new URL in the new site.
    var newurl = 'butterflies-child-care.click' 
   
  var response301 = {
        statusCode: 301,
        statusDescription: 'Moved Permanently',
        headers: {
            'location': { value: newurl },
            'cache-control' : { value: 'max-age=31536000, immutable' },
            'strict-transport-security' : { value: 'max-age=31536000; includeSubDomains' }
        }
    };

    if ( requestHeaders['host'].value !== newurl){
        console.log("Response: " + JSON.stringify(response301) + ".");
        return response301
    }

    // Default 
    return response;
}

This function will return a 301 to newurl, when presented with any request where the host header is not newurl.

Please test and ensure this code works, I wrote this without testing it in CloudFront - may have syntax and logic errors!

AWS
EXPERT
answered 7 months ago
  • Thank you for this idea. Unfortunately, I was unable to get it to work. The Test event was working and generating Cloudwatch logs but my request via a browser, was not triggering the Lambda Edge function. Eventually I got 503 errors (throttling) and gave up. The route I went with is: create an index.html file which specifies the redirect. It's slower but in my case sufficient in practice.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions