CloudFront function to redirect domain to other url

0

Hello,

I have a subdomain (let's call it read.acme.com) and Ineed to redirect it to** acme.net/read**. The subdomain is managed within the acme.com zone in Route 53. We do not have access to create a rewrite rule on acme.net to redirect incoming requests from read.acme.com to the /read URI (which would be the simple way to achieve this I suppose.

From the reading I've been doing, I've been led to believe the simplest way to achieve this would be to create a distribution for read.acme.com in CloudFront, and then associate a function to redirect incoming requests to acme.net/read.

Here's the function I created:

function handler(event) {
    var request = event.request;
    var headers = request.headers;
    var mainDomain = "acme.net";
    var uri = read

    if (headers.host.value !== mainDomain) {
        return {
            statusCode: 301,
            statusDescription: "Moved Permanently",
            headers: {
                location: { value: "https://" + mainDomain + uri },
            },
        };
    }

    return request;
}

The function fails testing with the message "read missing in 5".

I tried adding an alias to the read.acme.com record in Route 53 to point to a CloudFront distribution, but that failed with error

Error occurred
Alias Target contains an invalid value. (InvalidChangeBatch 400: "" is not a valid hosted zone id. is not a valid encrypted identifier)

I was able to add an alias Cname to point to the distribution URL.

Not sure what to make of this. Am I going in the right direction ? What am I missing ?

Thanks !

Update:

Thanks to Bryant Pollard for the script solution. If I enter the distribution URL in a browser it works fine now. However, I can't seem to get the DNS entry in Route 53 to work. If I try to do an A record pointing to the distribution, I'm still getting the message

Error occurred
Alias Target contains an invalid value. (InvalidChangeBatch 400: "" is not a valid hosted zone id. is not a valid encrypted identifier)

I can only create a cname entry pointing to the CloudFront distribution URL, but when I do that and then browse to read.acme.com I get the following:

403 ERROR
The request could not be satisfied.
Bad request. We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.

Generated by cloudfront (CloudFront)
Request ID: k4gMqQ4N1pgZQQ0cmfbyPxnVxzr6SqRLvyZocm95NhOvHp5pBZTXaQ==

It seems to have something to do with the region in which my CloudFront distribution is created vs where my zone is hosted, but I can't find where the CloudFront distribution is created. My zone is hosted in US East (N. Virginia).

Getting a little bit frustrated with the whole thing... :-(

Felix
질문됨 9달 전1071회 조회
2개 답변
1
수락된 답변

I've reviewed and did see 1 issue with the above code. Try adding a slash to this line:

location: { value: "https://" + mainDomain + "/" + uri },

It follows the example documented by Amazon: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/example-function-redirect-url.html

Hope this helps, if so please accept this answer

profile picture
답변함 9달 전
0

There is an example here with JavaScript that may help. https://aws.amazon.com/blogs/networking-and-content-delivery/handling-redirectsedge-part1/

It’s redirecting to a different page.

You may be able to use this and modify for your requirement.

profile picture
전문가
답변함 9달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠