Limited Time Free!  Experience website acceleration and advanced security protection!
Get Started Now 

Redirect based on the Location

In this example, an Edge Function is used to automatically redirect the client to the target URL corresponding to their region by identifying the client's region.

Code

const URLS = {
  JP: 'https://data.playground.edgeone.ai/resource/html/hello-world.ja-jp.html',
  US: 'https://data.playground.edgeone.ai/resource/html/hello-world.en-us.html',
  DEFAULT: 'https://data.playground.edgeone.ai/resource/html/hello-world.zh-cn.html'
};

function handleRequest(request) {
  const countryCode = request.eo?.geo?.countryCodeAlpha2;

  const url = URLS[countryCode] || URLS.DEFAULT;

  return Response.redirect(url, 302);
}

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request));
});

Preview

Xnip2024-07-16_11-32-23.png

References