Skip to content

Instantly share code, notes, and snippets.

@javanigus
Created September 29, 2023 01:05
Show Gist options
  • Save javanigus/702d820a7b3754c1f7b489715ec6a325 to your computer and use it in GitHub Desktop.
Save javanigus/702d820a7b3754c1f7b489715ec6a325 to your computer and use it in GitHub Desktop.
AWS CloudFront Function to Redirect URLs
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