简体   繁体   English

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

[英]Redirect root domain to www in aws route 53 without S3

I want to redirect my root domain to www.domain.com .我想将我的根域重定向到www.domain.com The site is published using s3 CloudFront and route53.该站点使用 s3 CloudFront 和 route53 发布。

I have seen a lot of docs on how to redirect using s3, but I cannot do it because there's already a bucket with my root domain somewhere.我已经看过很多关于如何使用 s3 进行重定向的文档,但我不能这样做,因为我的根域在某处已经有一个存储桶。 So I cannot create a bucket with my root domain as the name.所以我无法创建一个以我的根域为名称的存储桶。 Also, I doubt whether this s3 redirection will work for the https request.另外,我怀疑这个 s3 重定向是否适用于 https 请求。

I haven't seen any blog on redirecting without using the s3 bucket.我还没有看到任何关于在不使用 s3 存储桶的情况下进行重定向的博客。

So how can I redirect an HTTP/https root domain to www subdomain in aws.那么如何将 HTTP/https 根域重定向到 aws 中的 www 子域。

You cannot redirect using Route 53 (it is a DNS configuration service afterall, whereas redirects are a HTTP operation).您不能使用 Route 53 进行重定向(毕竟它是 DNS 配置服务,而重定向是 HTTP 操作)。

If you cannot use S3, another solution could be to use use CloudFront with a Lambda@Edge function.如果您不能使用 S3,另一种解决方案可能是将 CloudFront 与Lambda@Edge function 一起使用。

If the hostname is not the www domain you could perform the redirect to the www domain.如果主机名不是www域,您可以执行重定向www域。

The function might look similar to the below function 可能与以下类似

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM