Skip to content

Instantly share code, notes, and snippets.

@maraisr
Created July 28, 2021 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maraisr/e35dccf99edfa7b9ab847dc3b76e06d4 to your computer and use it in GitHub Desktop.
Save maraisr/e35dccf99edfa7b9ab847dc3b76e06d4 to your computer and use it in GitHub Desktop.
GeoRedirect CF Worker
const localeMap = {
AU: "/au",
US: "/us",
GB: "/uk",
NZ: "/nz",
MX: "/es-mx",
"*": "/au",
};
addEventListener(
"fetch",
(event, request) => {
const url = new URL(request.url);
if (url.pathname === "/") {
const locale = request.cf?.country ?? "*";
url.pathname = localeMap[locale] ?? localeMap["*"];
url.protocol = "https"; // be good
return void event.respondWith(Response.redirect(url.href, 307));
}
return void event.respondWith(fetch(request));
},
);
export {};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment