Skip to content

Instantly share code, notes, and snippets.

@moycat
Created March 15, 2020 07:58
Show Gist options
  • Save moycat/71b86e5bc8bafb58aaad0288c758020b to your computer and use it in GitHub Desktop.
Save moycat/71b86e5bc8bafb58aaad0288c758020b to your computer and use it in GitHub Desktop.
Why occupy a PAGE RULE when you can do it with a WORKER? For CloudFlare users.
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Respond to the request
* @param {Request} request
*/
async function handleRequest(request) {
url = new URL(request.url)
url.protocol = "https:"
url.hostname = url.hostname.replace(/^(www\.)/, "")
return Response.redirect(url, 301)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment