在没有 S3 的 aws route 53 中将根域重定向到 www

Redirect root domain to www in aws route 53 without S3

我想将我的根域重定向到 www.domain.com。该站点使用 s3 CloudFront 和 route53 发布。

我看过很多关于如何使用 s3 进行重定向的文档,但我做不到,因为某处已经有一个包含我的根域的存储桶。所以我无法创建一个以我的根域为名称的存储桶。另外,我怀疑这个 s3 重定向是否适用于 https 请求。

我还没有看到任何关于不使用 s3 存储桶进行重定向的博客。

那么如何将 HTTP/https 根域重定向到 aws 中的 www 子域。

您不能使用 Route 53 进行重定向(毕竟它是 DNS 配置服务,而重定向是 HTTP 操作)。

如果您不能使用 S3,另一种解决方案是使用具有 Lambda@Edge 功能的 CloudFront。

如果主机名不是 www 域,您可以对 www 域执行 redirect

该函数可能类似于以下内容

def lambda_handler(event, context):
 
    request = event["Records"][0]["cf"]["request"]
    response = event["Records"][0]["cf"]["response"]
    # Generate HTTP redirect response with 302 status code and Location header.
    
    if request['headers']['host'][0]['value'] != 'www.example.com':
     
        response = {
            'status': '302',
            'statusDescription': 'Found',
            'headers': {
                'location': [{
                    'key': 'Location',
                    'value': 'https://www.example.com/{}' .format(request['uri'])
                }]
            }
        }
     
    return response