Amazon S3 重定向规则 - 保留查询参数 [英] Amazon S3 Redirect Rule - Preserve Query Params

查看:33
本文介绍了Amazon S3 重定向规则 - 保留查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到 Amazon S3 重定向规则 - GET 数据丢失 但是在遵循接受的答案之后,我的查询参数仍然没有被保留.

I noticed Amazon S3 Redirect rule - GET data is missing but after following the accepted answer my query params still are not being preserved.

我有一个使用 React 和 React Router 的站点,这意味着我有几个 URL 加载相同的 HTML 和 JS,然后 JS 会根据 URL 确定要加载应用程序的哪个部分.

I have a site that uses React and React Router, meaning I have several URLs that load identical HTML and JS and then the JS figures out which part of the app to load based on the URL.

例如:

/foo,/bar,/baz 都应该加载 index.html,它加载 bundle.js.然后 bundle.js 观察 URL 并路由到一些 React 组件(也在 bundle.js 中).

/foo, /bar, /baz all should load index.html, which loads bundle.js. Then bundle.js observes the URL and routes to some React component (also in bundle.js).

然而,S3 中不存在 foo、bar 或 baz 文件,只有 index.html.我想要做的是当我收到 404 时,重定向到/#!/{URL}(例如/foo 重定向到/#!/foo).这适用于我的重定向规则(如下).但是,我也想带上查询参数(例如,/foo?ping=pong 重定向到/#!/foo?ping=pong)但/foo?ping=pong 只是重定向到/#!/foo.

However no foo, bar, or baz file exists in S3, only index.html. What I want to do is when I get a 404, redirect to /#!/{URL} (eg. /foo redirects to /#!/foo). This works fine with my redirect rule (below). However, I also want to bring query params with me (eg. /foo?ping=pong redirects to /#!/foo?ping=pong) but instead /foo?ping=pong just redirects to /#!/foo.

这是我的重定向规则:

<RoutingRules>
    <RoutingRule>
        <Condition>
            <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
        </Condition>
        <Redirect>
            <Protocol>http</Protocol>
            <HostName>www.mydomain.com</HostName>
            <ReplaceKeyPrefixWith>#!/</ReplaceKeyPrefixWith>
        </Redirect>
    </RoutingRule>
</RoutingRules>

我有什么想法可以实现这一目标吗?理想情况下,每次添加新页面时都不必在 S3/CloudFront 中更改某些内容?

Any ideas on some way I can achieve this? Ideally without having to go change something in S3/CloudFront every time I add a new page?

推荐答案

问题是我在 CloudFront 中设置了源不转发查询字符串,因此当 S3 收到请求时,它会在没有查询参数的情况下正确重定向.您可以在 CloudFront > 行为 > 转发查询字符串中找到此设置.

The problem was that I had the origin set up in CloudFront not to forward Query Strings so when S3 got the request it would redirect properly without the query params. You can find this setting in CloudFront > Behaviors > Forward Query Strings.

这篇关于Amazon S3 重定向规则 - 保留查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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