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

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

问题描述:

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

例如

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

是否有可能与一些CloudFront的或航线53配置?

你可以用几种方法做到这一点。

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端点。

API网关和λ:

将URL传递API网关与任何整合和到达LAMBDA,你可以返回相同的响应。

'use strict'; 

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

希望它有帮助。