/aで始まるパスを/path-aに置換すると同時に、後続のパス構造を保持します。パス正規表現置換により、古いパスを新しいパスにシームレスにマッピングできます。例えば、コンテンツ管理システムの改版時にリソースディレクトリ構造が変化する場合がありますが、正規表現のキャプチャグループを使用することで元のパス内の重要な部分を保持し、コンテンツへの継続的なアクセスを確保すると同時に、新しいディレクトリ構造に適応できます。async function handleEvent(event) {try {const request = event.request;const url = new URL(request.url);let pathname = url.pathname;// 正規表現を使用してパスを置換し、/aまたは/a/xxxを/path-aまたは/path-a/xxxに置換します。if (pathname.startsWith('/a')) {const aPathRegex = /^\\/a(\\/.*)?$/;pathname = pathname.replace(aPathRegex, '/path-a$1');}// パス大文字小文字変換、パス全体を直接小文字に変換します。if (pathname.startsWith('/b')) {pathname = pathname.toLowerCase();}url.pathname = pathname;// 新しいリクエストを作成します。const newRequest = new Request(url.toString(), {method: request.method,headers: request.headers,body: request.body,redirect: 'manual'});const response = await fetch(newRequest);return event.respondWith(response);} catch (err) {console.log(err);}}addEventListener('fetch', event => {handleEvent(event);});
/aで始まるパスを/path-aに置換し、同時に後続のパス構造を保持します。

フィードバック