question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

S3 redirect loses query string

See original GitHub issue

Hi, I’m having the following issue:

Say I have a page called nice-page. Currently, whenever I access a url without a forward slash at the end of its path, e.g.:

https://mywebsite.com/nice-page

S3 tries to find an object with that name (nice-page) but instead finds nothing, so it falls back to trying to find a folder with that name, therefore sending a 302 to the client and redirecting it to

https://mywebsite.com/nice-page/

(notice the forward slash at the end). This opens the page correctly, however, when the redirect happens, the browser loses all query string parameters, making we lose campaign traffic informations like utm_source, utm_campaign etc.

Do you guys have any idea of how to fix this?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

11reactions
JoshuaWalshcommented, Apr 1, 2019

@philippeehlert While testing my new terraform-aws-gatsby module I wrote a Lambda@Edge function which I believe achieves what you want. Attach this function to the Origin Response event:

'use strict';

exports.handler = (event, context, callback) => {
    var cf = event.Records[0].cf;
    var request = cf.request;
    var response = cf.response;

    if (response.status[0] === "3" && response.headers.location && response.headers.location[0] && request.querystring) {
        response.headers.location[0].value += "?" + request.querystring;
    }
    
    return callback(null, response);
};

You will need to ensure that on your CF distribution, “Query String Forwarding and Caching is set either to Forward All, Cache Based on Whitelist or to Forward All, Cache Based on All” as mentioned here.

This lambda function will preserve the query string for all 3XX responses. If you want to be more selective, you will have to modify the function.

0reactions
Tushar-Sawdayscommented, Jan 18, 2022

@JoshuaWalsh: What needs to be done to get the “301” in your code?

Read more comments on GitHub >

github_iconTop Results From Across the Web

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...
Read more >
S3 redirect to another domain and preserving Query String
The issue of losing the campaign query strings during the redirect is because of the element ReplaceKeyWith .
Read more >
Amazon S3 Bucket Redirect with Query Params - Tushar Ghate
Using an Amazon S3 Bucket to redirect to a URL with query parameters.
Read more >
Values that you specify when you create or update a distribution
Query string whitelist (Applies only when you choose Forward all, cache based on ... Redirect response from Amazon S3? and Temporary Request Redirection....
Read more >
Querystring parameters - HTTPie 3.2.1 (latest) docs
The initial URL is always used as the basis for the generated filename — even if there has been one or more redirects....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found