在 CloudFront Lambda@Edge 中检索用户域名以进行源请求(SEO 修改)

问题描述 投票:0回答:1

我正在尝试在由源请求触发的 CloudFront Lambda@Edge 函数中检索原始用户的域名。我需要在 S3 上托管的静态 Web 应用程序中进行 SEO 修改,但 CloudFront 似乎屏蔽了域名。如何访问用户的域名进行针对性的SEO调整?

我尝试过使用各种源请求策略选项,包括那些公开大多数标头的选项,但它们似乎都没有提供用户的域名

amazon-web-services amazon-cloudfront
1个回答
0
投票

假设您正在尝试查找客户端使用的域(即

host
标头中存在的信息。当使用 Viewer Request 触发时,
request.headers.host.value
行应该为您提供客户端使用的主机名。共享代码从
domain1.com
重定向到
domain2.com
以显示其正在使用中的示例。

async function handler(event) {
    const request = event.request
    const headers = request.headers
    const host = request.headers.host.value
    const uri = request.uri
    const newurl = 'https://domain2.com' + uri

    if (host === 'domain1.com') {
        console.log(host)
        console.log('redirecting to newsite')
        const response = {
            statusCode: 301,
            statusDescription: 'Moved Permanently',
            headers:
                { "location": { "value": newurl } }
            }

        return response // return the altered request
    }
    return request // allow everything else to continue to the origin
}
© www.soinside.com 2019 - 2024. All rights reserved.