2017-10-20 215 views
0

我在heroku上託管單頁應用程序並使用amazon cloudfront,路線53爲它。現在我想將一些內部路由重定向到其他路由而不觸及源代碼。如何使用Heroku/Amazon cloudfront/route 53重定向

例如

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

是否有可能與一些CloudFront的或航線53配置?

回答

1

你可以用幾種方法做到這一點。

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); 
}; 

希望它有幫助。