다음 뷰어 요청 함수는 요청이 특정 국가 내에서 오는 경우 뷰어를 국가별 URL로 리디렉션하는 응답을 생성합니다. 이 함수는 CloudFront-Viewer-Country
헤더의 값에 의존하여 최종 사용자의 국가를 결정합니다.
중요
이 예에서는 독일에 대한 최종 사용자 요청이 독일에서 오는 경우 최종 사용자를 독일에 고유한 URL로 리디렉션합니다. 최종 사용자 요청이 독일에서 오지 않는 경우 함수는 수정되지 않은 원래의 요청을 반환합니다.
async function handler(event) {
const request = event.request;
const headers = request.headers;
const host = request.headers.host.value;
const country = Symbol.for('DE'); // Choose a country code
const newurl = `https://${host}/de/index.html`; // Change the redirect URL to your choice
if (headers['cloudfront-viewer-country']) {
const countryCode = Symbol.for(headers['cloudfront-viewer-country'].value);
if (countryCode === country) {
const response = {
statusCode: 302,
statusDescription: 'Found',
headers:
{ "location": { "value": newurl } }
}
return response;
}
}
return request;
}
재작성 및 리디렉션에 대한 자세한 내용은 AWS 워크샵 스튜디오에서 엣지 함수를 사용하여 재작성 및 리디렉션 처리하기