async function handleEvent(event) {const { request } = event;const urlInfo = new URL(request.url);// Regular expression matches /test/ followed by 1 or 2 path segments that are not slashesconst regexp = /^\\/test\\/([^\\/]+)(?:\\/([^\\/]+))?\\/?$/;// Check if the path matches the regular expressionif (regexp.test(urlInfo.pathname)) {const matches = urlInfo.pathname.match(regexp);let newPathname = '/test/';// Construct a new pathname, which may be one or two according to the number of matched path segmentsnewPathname += matches[1]; // The first path segmentif (matches[2]) {newPathname += '/' + matches[2]; // The second path segment, if any}// Ensure it ends with index.htmlnewPathname += '/index.html';// Update the pathname of URLInfourlInfo.pathname = newPathname;}// Make a request using the updated URLInfoconst response = await fetch(urlInfo.toString(), {method: request.method,headers: request.headers,redirect: 'manual',body: request.body,});// return the response to the eventreturn event.respondWith(response);}// call the handleEvent function for each fetch eventaddEventListener('fetch', (event) => {handleEvent(event);});
https://www.example.com/test/segment1/index.html
https://www.example.com/test/segment1/segment2/index.html

피드백