Amazon S3 Redirection Rule - Saving Params Requests

I noticed the Amazon S3 redirection rule - there is no GET data , but after the accepted response, my request parameters are still not saved.

I have a website that uses React and React Router, that is, I have several URLs that load the same HTML and JS, and then JS determines which part of the application to load based on the URL.

For example:

/ foo, / bar, / baz should all load index.html, which loads bundle.js. Bundle.js then looks at the URL and routes any React component (also in bundle.js).

However, in S3 there is no foo, bar or baz file, but only index.html. What I want to do is when I get 404, redirect to / #! / {URL} (e.g.. / Foo redirect to / #! / Foo). This works fine with my redirection rule (below). However, I also want to bring request parameters with me (e.g. / foo? Ping = pong redirects to / #! / Foo? Ping = pong), but instead / foo? Ping = pong just redirects to / #! / Foo.

Here are my redirection rules:

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

Any ideas somehow I can achieve this? Ideally, without having to change anything in S3 / CloudFront every time I add a new page?

+8
redirect amazon-s3 amazon-web-services amazon-cloudfront react-router
source share
2 answers

The problem was that I had a source created in CloudFront so as not to forward the request lines, so when S3 received the request, it will redirect correctly without request parameters. This option can be found in CloudFront> Behaviors> Forward Query Strings.

+17
source share

If you want to have a clear URL, you can also check this trick . You need to configure the distribution of the cloud network, and then change the behavior of 404 in the Error Pages section of your distribution. That way you can again domain.com/foo/bar links :)

+2
source share

All Articles