Skip to content

Instantly share code, notes, and snippets.

@patilvikasj
Last active September 26, 2023 14:40
Show Gist options
  • Select an option

  • Save patilvikasj/ba5b156762e71fe8a70657d1683834b0 to your computer and use it in GitHub Desktop.

Select an option

Save patilvikasj/ba5b156762e71fe8a70657d1683834b0 to your computer and use it in GitHub Desktop.
Redirection using CloudFlare workers
const base = "https://example.com";
const statusCode = 301;
async function handleRequest(request) {
const url = new URL(request.url);
let { pathname } = url;
// Split the pathname into parts using '/'
var parts = pathname.split('/');
var firstPart = parts[1];
var secondPart = parts[2];
// you can modify this condition as per your need.
if( 'plugins' === firstPart || 'themes' === firstPart ) {
const destinationURL = base + "?" + firstPart + "=" + secondPart;
return Response.redirect(destinationURL, statusCode)
}
return fetch(request)
}
addEventListener("fetch", async event => {
event.respondWith(handleRequest(event.request))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment