Amazon S3重定向规则 - 保留查询参数

时间:2022-11-18 23:04:22

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

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

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.

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

For example:

例如:

/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).

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

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.

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

Here are my redirect rules:

这是我的重定向规则:

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

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?

我能以某种方式获得任何想法吗?理想情况下,每次添加新页面时都不必在S3 / CloudFront中进行更改?

2 个解决方案

#1


22  

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.

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

#2


2  

If you want to have clear url's though you can also check out this trick. You need to setup cloudfront distribution and then alter 404 behaviour in "Error Pages" section of your distribution. That way you can again domain.com/foo/bar links :)

如果你想要有清晰的网址,你也可以查看这个技巧。您需要设置cloudfront分发,然后在分发的“错误页面”部分中更改404行为。这样你可以再次domain.com/foo/bar链接:)

#1


22  

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.

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

#2


2  

If you want to have clear url's though you can also check out this trick. You need to setup cloudfront distribution and then alter 404 behaviour in "Error Pages" section of your distribution. That way you can again domain.com/foo/bar links :)

如果你想要有清晰的网址,你也可以查看这个技巧。您需要设置cloudfront分发,然后在分发的“错误页面”部分中更改404行为。这样你可以再次domain.com/foo/bar链接:)