Skip to content

Instantly share code, notes, and snippets.

@javanigus
Created October 7, 2023 16:33
Show Gist options
  • Save javanigus/44276938816437c47639f2832e2dd2d4 to your computer and use it in GitHub Desktop.
Save javanigus/44276938816437c47639f2832e2dd2d4 to your computer and use it in GitHub Desktop.
AWS Lambda Function to Redirect HTTP Requests
export const handler = async (event, context) => {
const request = event.request;
var newLocation = "";
var statusCode = 301;
var statusDescription = "Moved Permanently";
switch(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 request;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment