Skip to content

Instantly share code, notes, and snippets.

@ambanmba
Created March 28, 2022 02:38
Show Gist options
  • Save ambanmba/f731c02fd14087e111b5f367ba114bbf to your computer and use it in GitHub Desktop.
Save ambanmba/f731c02fd14087e111b5f367ba114bbf to your computer and use it in GitHub Desktop.
cf-rewrite-au-to-com-au
const statusCode = 301;
async function handleRequest(request) {
const url = new URL(request.url);
const hostname = url.hostname;
const { pathname, search } = url;
const askhostname = /(.au)/;
const gethostname = `.com.au`;
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

Automatically rewrites anything.au to anything.com.au

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