AWS CloudFront重定向到路径 [英] AWS CloudFront redirect to path

查看:69
本文介绍了AWS CloudFront重定向到路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CloudFront发行版,可将www.myapp.com重定向到存储我的网站的S3存储桶.该发行版使用CName www.myapp.com和默认根对象"index.html".

I have a CloudFront distribution that redirects www.myapp.com to my S3 bucket where my website is stored. The distribution uses CName www.myapp.com and Default Root Object "index.html".

我还希望将www.myapp.com/path重定向到同一存储桶,以便用户可以通过www.myapp.com和www.myapp.com/path进入我的网站.

I want to also redirect www.myapp.com/path to the same bucket so users can either get to my website through www.myapp.com and www.myapp.com/path.

有人知道如何使用CloudFront将自定义路径重定向到存储桶吗?

Anyone has any idea how to use CloudFront to redirect a custom path to bucket?

推荐答案

使用/path/的行为和针对原始请求的lambda函数解决了该问题:

Solved it using a behavior for /path/ and a lambda function on origin request:

exports.handler = async (event, context, callback) => {
    const response = {
        status: '301',
        statusDescription: 'Moved Permanently',
        headers: {
            location: [{
                key: 'Location',
                value: "https://" + event.Records[0].cf.request.headers.host[0].value.replace(".s3.amazonaws.com", ""),
            }],
        },
    };

    callback(null, response);
};

这篇关于AWS CloudFront重定向到路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆