Amazon S3 Redirect Rule - Preserve Query Params
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.
If you want to have clear urls 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 :)
The menus and options in CloudFront/S3 change a lot over time.
Here is a December 2021 solution.
Step 1) Create a "Request" Policy in CloudFront that allows QueryStrings
Note: you might want to also add some Headers like Origin
or Access-Control-
... headers for CORS.
Step 2) Go to your Distribution > Update the Origin request policy
Step 3) Kick a new Invalidation on /*
Additional Notes for Debuging/Testing
- I would recommend testing with
curl
in terminal rather than a browser to avoid caching and also seeing the details. I docurl -v https://example.com/cb?foo=bar1
. - Keep increasing the value of the query string (
bar1
in the above example, tobar2
,bar3
) with every test to make such there is no caching again.