Last active
April 6, 2018 22:52
-
-
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.
This file contains hidden or 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
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