async function handleRequest(request) {
const yourExternalHostname = "www.example.com";
const redirectMap = new Map([
["/foo", "https://" + yourExternalHostname + "/redirect1"],
["/bar", "https://" + yourExternalHostname + "/redirect2"],
["/baz", "https://" + yourExternalHostname + "/redirect3"],
]);
const url = new URL(request.url);
const path = url.pathname;
if (redirectMap.has(path)) {
return Response.redirect(redirectMap.get(path), 301);
} else {
return new Response('Not Found', { status: 404 });
}
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
本页内容是否解决了您的问题?