ビューワーを新しい URL にリダイレクトさせる - Amazon CloudFront

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

ビューワーを新しい URL にリダイレクトさせる

この例では、リクエストが特定の国内から送信されたときに、ビューワーを国固有の URL にリダイレクトするレスポンスを生成します。この関数は、CloudFront-Viewer-Country ヘッダーの値に依存して、ビューワーの国を判断します。

重要

この関数を機能させるには、キャッシュポリシーまたはオリジンリクエストポリシーの許可された CloudFront CloudFront-Viewer-Countryヘッダーに追加して、受信リクエストに ヘッダーを追加するように を設定する必要があります。 オリジンリクエストの制御

この例では、ビューワーリクエストがドイツから送信された場合、ビューワーをドイツ固有の URL にリダイレクトします。ビューワーリクエストがドイツから来ていない場合、この関数は元の変更されていないリクエストを返します。

これはビューワーリクエスト機能です。

でこの例を参照してください GitHub

JavaScript runtime 2.0
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; }
JavaScript runtime 1.0
function handler(event) { var request = event.request; var headers = request.headers; var host = request.headers.host.value; var country = 'DE' // Choose a country code var newurl = `https://${host}/de/index.html` // Change the redirect URL to your choice if (headers['cloudfront-viewer-country']) { var countryCode = headers['cloudfront-viewer-country'].value; if (countryCode === country) { var response = { statusCode: 302, statusDescription: 'Found', headers: { "location": { "value": newurl } } } return response; } } return request; }

書き換えとリダイレクトの詳細については、AWSワークショップスタジオの「エッジ関数を使用した書き換えとリダイレクトの処理」を参照してください。