Country code redirect
Redirect a response based on the country code in the header of a visitor.
Last updated Oct 13, 2025
export default {
async fetch(request) {
/**
* A map of the URLs to redirect to
* @param {Object} countryMap
*/
const countryMap = {
// Replace the country codes and target URLs with ones that apply to your case.
US: "https://example.com/us",
EU: "https://example.com/eu",
};
// Use the cf object to obtain the country of the request
// more on the cf object: https://developers.cloudflare.com/workers/runtime-apis/request#incomingrequestcfproperties
const country = request.cf.country;
// If country is not null and is defined in the country map above, redirect.
if (country != null && country in countryMap) {
const url = countryMap[country];
// Remove this logging statement from your final output.
console.log(
`Based on ${country}-based request, your user would go to ${url}.`,
);
return Response.redirect(url);
// If request country not in map, return another page.
} else {
return fetch("https://example.com", request);
}
},
};
Was this helpful?
YesNo
On this page
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/rules/snippets/examples/country-code-redirect/#page","headline":"Country code redirect","description":"Redirect a response based on the country code in the header of a visitor.","url":"https://developers.cloudflare.com/rules/snippets/examples/country-code-redirect/","inLanguage":"en","image":"https://developers.cloudflare.com/rules/snippets/examples/country-code-redirect/og.png?v=59e4cb9f41a7e592","dateModified":"2025-10-13","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"},"keywords":["Localization","Redirects"]}
description: Store, retrieve, and remove assets from cache programmatically. Use this template to optimize performance and implement custom caching strategies. title: Custom cache image: https://developers.cloudflare.com/rules/snippets/examples/custom-cache/og.png?v=a5082cc5d1b0fcc3
Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/rules/llms.txt
Use this file to discover all available pages before exploring further.
Docs