Closed renzit closed 1 year ago
I ended with this code for all my needs:
function handler(event) {
var shouldRedirect = false;
var request = event.request;
var queryString = queryStringObjectToString(request.querystring);
var uri = request.uri;
var regex = /[A-Z]/g;
var headers = request.headers;
var host = headers.host.value;
var last_uri_character = uri.slice(-1);
// If the URI have a dot we assume is a file and we don't redirect or rewrite
if (uri.includes(".")) {
return request;
}
// If the URI contains a capital letter, rewrite to the lowercase version
if (regex.test(uri)) {
uri = request.uri.toLowerCase();
request.uri = uri;
}
// If the host is www we redirect to the naked domain
if (host.startsWith("www.")) {
host = host.replace("www.", "");
delete headers.host;
shouldRedirect = true;
}
// If the URI ends with a slash, redirect to the non-slash version
if (last_uri_character !== "/") {
uri = uri + "/";
shouldRedirect = true;
}
// If we should redirect, do it
if (shouldRedirect) {
var url = "https://" + host + uri + queryString;
var response = {
statusCode: 301,
statusDescription: "Permanently Moved",
headers: { location: { value: url } },
cookies: request.cookies,
};
return response;
}
return request;
}
/**
* @description: Converts an event.request.querystring object back to a query string
* @date 2022-11-28
* @param {object} queryStringObject: Query string object as provided by event.request
* @return {string}: A normalised query string in the form ?key1=valn&key2=val2...
*/
function queryStringObjectToString(queryStringObject) {
// Convert the query string object to an array of entries and reduce to a single string
return (
Object.entries(queryStringObject)
.reduce((p, q) => {
if (!q[1].multiValue) {
// Process a single key/value property
return (p += q[0] + "=" + q[1].value + "&");
} else {
// Process multiValue properties. E.g arrays
return (p +=
q[1].multiValue.map((v) => q[0] + "=" + v.value).join("&") + "&");
}
}, "?")
// Remove the trailing ampersand
.slice(0, -1)
);
}
Thanks.
The docs says that is possible to use a rewrite, but there isn't any example on how to achieve that CloudFront Functions Docs
I tried to change the request, in the test tab it works but no in the cloudfront distribution.
Code:
Test in cloudfront Function:
Test against distribution URL: