Created
September 29, 2023 01:05
-
-
Save javanigus/702d820a7b3754c1f7b489715ec6a325 to your computer and use it in GitHub Desktop.
AWS CloudFront Function to Redirect URLs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function handler(event) { | |
// NOTE: This example function is for a viewer request event trigger. | |
// Choose viewer request for event trigger when you associate this function with a distribution. | |
var newLocation = ""; | |
var statusCode = 301; | |
var statusDescription = "Moved Permanently"; | |
switch(event.request.uri) { | |
case "/test-pdf-1.pdf": | |
newLocation = "/test-pdf-2.pdf"; | |
statusCode = 301; | |
break; | |
case "/test-pdf-2.pdf": | |
newLocation = "https://www.google.com"; | |
statusCode = 302; | |
break; | |
} | |
if (statusCode === 302) { | |
statusDescription = "Found"; | |
} | |
if (newLocation !== "") { | |
var response = { | |
statusCode: statusCode, | |
statusDescription: statusDescription, | |
headers: { | |
'location': { value: newLocation } | |
} | |
}; | |
return response; | |
} else { | |
return event.request; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment