将查看器重定向到新的 URL
以下示例函数会生成一个响应,以便在请求来自特定国家/地区时将查看器重定向到特定国家/地区的 URL。此函数依赖 CloudFront-Viewer-Country
标头的值来确定查看器所在的国家/地区。
当查看器请求来自德国时,此示例将查看器重定向到德国特定的 URL。如果查看器请求不是来自德国,该函数将返回原始的未修改请求。
这是查看器请求函数。
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; }