1 Answer
- Newest
- Most votes
- Most comments
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!
Relevant content
- asked 2 years ago
- asked 2 years ago
- asked 2 years ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 months ago
- AWS OFFICIALUpdated 2 years 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.