次のコード例は、CloudFront Functions ビューワーリクエストイベントで新しい URL にリダイレクトする方法を示しています。
- CloudFront Functions の JavaScript ランタイム 2.0
-
注記
GitHub には、その他のリソースもあります。用例一覧と設定および実行方法については、CloudFront Functions の例
リポジトリをご覧ください。 async 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 SDK デベロッパーガイドとコード例の詳細なリストについては、「AWS SDK での CloudFront の使用」を参照してください。このトピックには、使用開始方法に関する情報と、以前の SDK バージョンの詳細も含まれています。