重定向到 CloudFront Functions 查看器请求事件中的新 URL - Amazon CloudFront
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

重定向到 CloudFront Functions 查看器请求事件中的新 URL

以下代码示例演示了如何重定向到 CloudFront Functions 查看器请求事件中的新 URL。

JavaScript
适用于 CloudFront Functions 的 JavaScript 运行时 2.0
注意

查看 GitHub,了解更多信息。查找完整示例,并了解如何在 CloudFront Functions examples 存储库中进行设置和运行。

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; }

有关 Amazon SDK 开发人员指南和代码示例的完整列表,请参阅 将 CloudFront 与 Amazon SDK 配合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。