Last active
September 26, 2023 14:40
-
-
Save patilvikasj/ba5b156762e71fe8a70657d1683834b0 to your computer and use it in GitHub Desktop.
Redirection using CloudFlare workers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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