I need to write an AWS CloudFront Function that does the following:
Splits the incoming request URI into path and query string components.
Modifies the path while keeping the query string intact.
Sets a custom header with a specific part of the path from the original URI.
I’m not sure how to go about implementing this. Can someone provide guidance or an example function that accomplishes these tasks? I’d appreciate any help or pointers on how to achieve this.
Thanks in advance!!!
I found the solution myself. Here’s the working CloudFront function that performs the required tasks:
function handler(event) {
const request = event.request;
const uri = request.uri;
const rawHeaderValue = uri.split('/')[1];
const headerValue = rawHeaderValue.split('?')[0];
request.headers['x-path'] = { value: headerValue }
request.uri = uri.replace('/' + headerValue, '');
if (request.uri === '') {
request.uri = '/'
}
return request;
}
This function splits the URI, modifies the path, and sets the custom header as needed.
Pueblo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.