aws-samples / aws-samples/amazon-cloudfront-functions

Redirect preserve query string

Open
#11 12 comments 9 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
533
Forks
76
PR merge metrics
No merged PRs in 30d

Description

This relates to issue #7 . I've used the code from there and made it more generic and fixed a couple of edge-cases that wouldn't have worked. Please add this to sample library as it's a very common use-case with a non-obvious solution (for those of us not js experts).

```
function objectToQueryString(obj) {
var str = [];
for (var param in obj)
if (obj[param].value == '')
str.push(encodeURIComponent(param));
else
str.push(encodeURIComponent(param) + "=" + encodeURIComponent(obj[param].value));

return str.join("&");
}

function handler(event) {
var request = event.request;
var uri = request.uri;
var loc = "";
var newdomain = newdomain.com;

if (Object.keys(request.querystring).length)
loc = `https://${newdomain}${uri}?${objectToQueryString(request.querystring)}`
else
loc = `https://${newdomain}${uri}`

var response = {
statusCode: 302,
statusDescription: 'Found',
headers: {
'location': { value: `${loc}` }
}
};
return response;
}

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.