Skip to content

Instantly share code, notes, and snippets.

@peterlozano
Last active April 6, 2018 22:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterlozano/455c6d67f0e8d99aa9326ef49e6d6c78 to your computer and use it in GitHub Desktop.
Save peterlozano/455c6d67f0e8d99aa9326ef49e6d6c78 to your computer and use it in GitHub Desktop.
Redirect to www domain using AWS CloudFront Lambda @ Edge. Attach this function to the Viewer Request event.
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
if (request.headers['host'][0].value !== 'www.example.com') {
/*
* Generate HTTP redirect response with 302 status code and Location header.
*/
const response = {
status: '302',
statusDescription: 'Found',
headers: {
location: [{
key: 'Location',
value: 'https://www.example.com',
}],
},
};
callback(null, response);
}
else {
callback(null, request);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment