tencent cloud

문서Tencent Cloud EdgeOne

Performing Redirect Based on the Request Location

다운로드
포커스 모드
폰트 크기
마지막 업데이트 시간: 2026-11-04 10:29:58
AI 번역 및 품질 검수 완료
This sample determines the client's region and automatically redirects the client to the target URL of that region. It implements request distribution through edge functions based on the client's region.

Sample Code

// All region URL sets
const urls = {
CN: 'https://www.example.com/zh-CN',
US: 'https://www.example.com/en-US',
};

// Default redirect URL
const defaultUrl = 'https://www.example.com/en-US';

/**
* Redirect to the target URL based on the region of the current request
* @param { Request } request
*/
function handleRequest(request) {
// Get the region of the current request
const alpha2code = request.eo.geo.countryCodeAlpha2;
// Redirect target URL
const url = urls[alpha2code] || defaultUrl;

return Response.redirect(url, 302);
}

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

Sample Preview

Enter a URL that matches an edge function trigger rule in the browser address bar to preview the sample effect.


Related References

도움말 및 지원

문제 해결에 도움이 되었나요?

피드백