带有Lambda的CloudFront重定向请求到尾部斜杠 [英] CloudFront redirect request with Lambda to trailing slash

查看:124
本文介绍了带有Lambda的CloudFront重定向请求到尾部斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态SPA页面,它使用S3作为CloudFront的来源.如果我访问www.domain.com/page,将会得到CloudFront前缀为bucket-directory/prod/page/的路径.

I have a static SPA page that is using S3 as it's origin with CloudFront. If I visit www.domain.com/page, I will get the CloudFront path prefixed bucket-directory/prod/page/ which is expected.

是否有可能在AWS Lambda中捕获路径并将尾部斜杠附加到请求,所以它变为www.domain.com/page> [Lambda]> www.domain.com/page/

Is it possible to capture the path in AWS Lambda and append the trailing slash to a request, so it becomes, www.domain.com/page > [Lambda] > www.domain.com/page/

我一直在寻找并尝试以下资源而无济于事: http://blog.rowanudell.com/redirects-in-serverless/

I've been looking and trying the following resources to little avail: http://blog.rowanudell.com/redirects-in-serverless/

推荐答案

在Cloudfront上为SPA或静态页面进行完美设置并非易事.简而言之,您将需要(至少)为您的CF发行版使用一个原始请求lambda函数设置.您需要处理一些边缘情况,例如:

Getting a perfect setup for a SPA or static page on Cloudfront is not trivial. In short, you will need to use (at least) an origin request lambda function setting for your CF distro. You need to handle a few edge cases like:

  • 哈希
  • 重定向到带有斜杠(您提到过)的网址
  • 重定向时转发查询参数

要获得快速启动功能,可以查看本文

For a quick starting function you can check this article https://tinyendian.com/articles/better-origin-response-function-for-cloudfront-hosted-static-pages explaining the actual code that you can copy from here https://gist.github.com/karolmajta/6aad229b415be43f5e0ec519b144c26e

当然,当您的应用程序更改时,您可能需要在此处和此处修改此代码段,以满足您的需求.

Of course it is likely that as your app changes, you will need to modify this snippet here and there to match your needs.

这篇关于带有Lambda的CloudFront重定向请求到尾部斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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