Created
March 28, 2022 02:38
-
-
Save ambanmba/f731c02fd14087e111b5f367ba114bbf to your computer and use it in GitHub Desktop.
cf-rewrite-au-to-com-au
This file contains 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 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)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Automatically rewrites anything.au to anything.com.au