Skip to content

Instantly share code, notes, and snippets.

@ambanmba
Created March 28, 2022 02:05
Show Gist options
  • Save ambanmba/3bdfc7ad6f8937b8ed7a1508fd8030e0 to your computer and use it in GitHub Desktop.
Save ambanmba/3bdfc7ad6f8937b8ed7a1508fd8030e0 to your computer and use it in GitHub Desktop.
const statusCode = 301;
async function handleRequest(request) {
const url = new URL(request.url);
const hostname = url.hostname;
const { pathname, search } = url;
const askhostname = /(greenllgold.com)/;
const gethostname = `greeniigold.com`;
const newbase = hostname.replace(askhostname, gethostname);
const destinationURL = 'https://' + newbase + pathname + search;
return Response.redirect(destinationURL, statusCode);
}
addEventListener('fetch', async event => {
event.respondWith(handleRequest(event.request));
});
@ambanmba
Copy link
Author

ambanmba commented Mar 28, 2022

Example to silently redirect a typo domain to the correct domain using a Cloudflare Worker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment