---
title: Country code redirect
description: Redirect a response based on the country code in the header of a visitor.
image: https://edgetunnel-b2h.pages.dev/core-services-preview.png
---

> Documentation Index  
> Fetch the complete documentation index at: https://edgetunnel-b2h.pages.dev/rules/llms.txt  
> Use this file to discover all available pages before exploring further. 

[Skip to content](#%5Ftop) 

# Country code redirect

Redirect a response based on the country code in the header of a visitor.

**JavaScript**

```js
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://edgetunnel-b2h.pages.dev/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);
    }
  },
};
```

```json
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://edgetunnel-b2h.pages.dev/rules/snippets/examples/country-code-redirect/#page","headline":"Country code redirect · Cloudflare Rules docs","description":"Redirect a response based on the country code in the header of a visitor.","url":"https://edgetunnel-b2h.pages.dev/rules/snippets/examples/country-code-redirect/","inLanguage":"en","image":"https://edgetunnel-b2h.pages.dev/core-services-preview.png","dateModified":"2025-10-13","publisher":{"@type":"Organization","name":"Cloudflare","url":"https://www.cloudflare.com/"},"isPartOf":{"@type":"WebSite","@id":"https://edgetunnel-b2h.pages.dev/#website","name":"Cloudflare Docs","url":"https://edgetunnel-b2h.pages.dev/"},"keywords":["Localization","Redirects"]}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"/directory/","name":"Directory"}},{"@type":"ListItem","position":2,"item":{"@id":"/rules/","name":"Rules"}},{"@type":"ListItem","position":3,"item":{"@id":"/rules/snippets/","name":"Cloudflare Snippets"}},{"@type":"ListItem","position":4,"item":{"@id":"/rules/snippets/examples/","name":"Snippets examples"}},{"@type":"ListItem","position":5,"item":{"@id":"/rules/snippets/examples/country-code-redirect/","name":"Country code redirect"}}]}
```
