From d4890b870453731ba444fd5c81d9ecc2ecc41d2f Mon Sep 17 00:00:00 2001 From: joknoxy <76135626+joknoxy@users.noreply.github.com> Date: Tue, 30 Nov 2021 14:56:04 +1100 Subject: [PATCH 1/3] Initial commit --- redirect_maintain_uri_querystring/index.js | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 redirect_maintain_uri_querystring/index.js diff --git a/redirect_maintain_uri_querystring/index.js b/redirect_maintain_uri_querystring/index.js new file mode 100644 index 0000000..eca1d9a --- /dev/null +++ b/redirect_maintain_uri_querystring/index.js @@ -0,0 +1,34 @@ +function objectToQueryString(obj) { + var str = []; + for (var param in obj) + if (obj[param].multiValue) + str.push(param + "=" + obj[param].multiValue.map((item) => item.value).join(',')); + else if (obj[param].value == '') + str.push(param); + else + str.push(param + "=" + obj[param].value); + + return str.join("&"); +} + +function handler(event) { + var request = event.request; + var headers = request.headers; + 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; +} From 007ae055065d6a0ff8588fec7bf2d200905d60b7 Mon Sep 17 00:00:00 2001 From: joknoxy <76135626+joknoxy@users.noreply.github.com> Date: Tue, 30 Nov 2021 15:21:25 +1100 Subject: [PATCH 2/3] Initial commit --- redirect_maintain_uri_querystring/readme.md | 28 +++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 redirect_maintain_uri_querystring/readme.md diff --git a/redirect_maintain_uri_querystring/readme.md b/redirect_maintain_uri_querystring/readme.md new file mode 100644 index 0000000..6ed6383 --- /dev/null +++ b/redirect_maintain_uri_querystring/readme.md @@ -0,0 +1,28 @@ +## Redirect (temporary) to new domain + +**CloudFront Functions event type: viewer response** + +This function will perform a redirect (302 - temporary) to a new domain while maintaining the uri and query string values in the destination FQDN. + +If you want a permanent redirect, change statusCode from 302 to 301 in function code. + +**Important: Set the `newdomain` variable to an appropriate value for your specific needs.** + +**Testing the function** + +To validate that the function is working as expected, you can use the provided test object from https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-event-structure.html#functions-event-structure-example and test in the console using the instructions https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/test-function.html#test-function-console + +If the function has been set up correctly, you should see a result similar to the following with the value of 'newdomain', and the uri and querystring values from the test object being added in the 'location' response header in `FunctionOutput` JSON object: +``` +{ + "response": { + "headers": { + "location": { + "value": "https://newdomain.com/media/index.mpd?ID=42&Exp=1619740800&TTL=1440&querymv=val1,val2,val3" + } + }, + "statusDescription": "Found", + "cookies": {}, + "statusCode": 302 + } + ... From 6e46e8cb658b669ea92da77536a1a424363549a9 Mon Sep 17 00:00:00 2001 From: joknoxy <76135626+joknoxy@users.noreply.github.com> Date: Tue, 30 Nov 2021 15:23:19 +1100 Subject: [PATCH 3/3] Update readme.md --- redirect_maintain_uri_querystring/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redirect_maintain_uri_querystring/readme.md b/redirect_maintain_uri_querystring/readme.md index 6ed6383..51bbc68 100644 --- a/redirect_maintain_uri_querystring/readme.md +++ b/redirect_maintain_uri_querystring/readme.md @@ -10,7 +10,7 @@ If you want a permanent redirect, change statusCode from 302 to 301 in function **Testing the function** -To validate that the function is working as expected, you can use the provided test object from https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-event-structure.html#functions-event-structure-example and test in the console using the instructions https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/test-function.html#test-function-console +To validate that the function is working as expected, you can use the provided test object from https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-event-structure.html#functions-event-structure-example and test in the console using the instructions https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/test-function.html#test-function-console. If the function has been set up correctly, you should see a result similar to the following with the value of 'newdomain', and the uri and querystring values from the test object being added in the 'location' response header in `FunctionOutput` JSON object: ```