Reputation: 11661
I want to do rewrite in lambda@egde.
I got an error when I try to browse: https://example.com/foo
This is my function:
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
if (/^\/foo($|\/)/.test(request.uri)) {
request.uri = "https://api.example.com/bar";
}
callback(null, request);
};
I want to do rewrite (not redirect). What is wrong with this code?
Upvotes: 0
Views: 129
Reputation: 35238
You're changing the URI to be an absolute path of a users request, it should just be the path part of the request.
So the below is what this should become.
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
if (/^\/foo($|\/)/.test(request.uri)) {
request.uri = "/bar";
request.headers['host'] = [{ key: 'host', value: "api.example.com"}];
}
callback(null, request);
};
If you wanted to change the domain you would need to modify the host header.
Bare in mind with this example it will only rewrite the URI before it goes to the origin. If you want the users request to change you would need to perform a rewrite.
Upvotes: 1