如何使用Heroku / Amazon cloudfront / route 53进行重定向

时间:2017-10-20 09:21:14

标签: heroku amazon-cloudfront amazon-route53

我在heroku上托管单页应用程序并使用amazon cloudfront,路由53。现在我想将一些内部路由重定向到其他路由而不触及源代码。

例如

http://example.com/foo -> http://example.com/bar

是否可以使用某些cloudfront或Route 53配置?

1 个答案:

答案 0 :(得分:1)

你可以通过多种方式实现。

<强> LAMBDA @边:

您可以为查看者请求创建lambda边缘函数并执行重定向。

'use strict';
exports.handler = (event, context, callback) => {
    /*
     * Generate HTTP redirect response with 302 status code and Location header.
     */
    const response = {
        status: '302',
        statusDescription: 'Found',
        headers: {
            location: [{
                key: 'Location',
                value: 'http://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html',
            }],
        },
    };
    callback(null, response);
};

<强>参考: http://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html

<强> API-网关:

创建一个http代理并执行重定向到所需的URL。 您还需要创建源并将行为从cloudfront关联到此api-gateway端点。

使用Lambda的API网关:

使用 ANY 集成将网址传递给API网关,然后转到Lambda,您可以返回相同的响应。

'use strict';

exports.handler = function(event, context, callback) {
    var response = {
        statusCode: 301,
        headers: {
            "Location" : "https://example.com"
        },
        body: null
    };
    callback(null, response);
};

希望它有所帮助。