Redirecting Visitors by Country in Cloudflare

Redirecting Visitors by Country in Cloudflare

Cloudflare provides powerful tools for managing traffic on your website, including the ability to redirect visitors based on their geographic location. In this blog, we’ll walk you through setting up a redirect for users visiting from a specific country. For example, if a user from a specific country visits https://www.example.com/our-team, they can be redirected to https://www.example.co.ke/our-team.


Why Use Country-Based Redirects?

Country-based redirects are useful for scenarios such as:

  • Delivering localized content tailored to specific regions.
  • Redirecting users to a country-specific domain.
  • Complying with regional legal requirements or business operations.

Steps to Set Up a Country-Based Redirect in Cloudflare

1. Log in to Your Cloudflare Dashboard

  1. Navigate to Cloudflare and log in.
  2. Select the domain where you want to apply the redirect rule.

2. Go to the Redirect Rules Section

  1. In the left-hand menu, click on Rules.
  2. Select Redirect Rules.
  3. Click on Create a New Rule.

3. Set the Rule Name

  • Give your rule a descriptive name, such as Country Redirect.

4. Define the Conditions for the Redirect

a) Match by Country

  1. Under Field, choose Country.
  2. Set the Operator to equals.
  3. Enter the Value as the two-letter country code (e.g., US, KE, GB).

b) Match by URL

  1. Under URI, choose Strict Wildcard.
  2. Enter the wildcard pattern as: https://www.example.com/* This matches all pages under https://www.example.com/.

5. Preview the Expression

Your filter expression should look like this:

(ip.geoip.country eq "<COUNTRY_CODE>" and http.request.uri strict wildcard r"https://www.example.com/*")

6. Define the Redirect Action

  1. Set the Action to URL Redirect.
  2. Choose Type as Dynamic.
  3. Enter the Expression: https://www.example.co.<COUNTRY_CODE>/${http.request.uri.path} This ensures that the path after the domain is preserved. For example:
    • Request: https://www.example.com/our-team
    • Redirect: https://www.example.co.<COUNTRY_CODE>/our-team
  4. Set the Status Code to 308 (Permanent Redirect).
  5. Enable Preserve Query String to ensure query parameters are passed to the new URL.

7. Set Rule Priority

  • Place the rule First to ensure it is applied before any other rules.

8. Save and Test the Rule

  1. Save your rule.
  2. Test the redirect by visiting https://www.example.com from the specified country’s IP address.

Example Use Case

Suppose a user from a specific country visits https://www.example.com/our-team. The redirect rule will automatically send them to https://www.example.co.<COUNTRY_CODE>/our-team. Similarly, users accessing other pages (e.g., https://www.example.com/about) will be redirected to the corresponding country-specific version.


Best Practices

  • Use Strict Wildcard for precise matching to ensure the rule applies to the intended URLs.
  • Test the redirect using a VPN or a tool like Geopeeker to emulate requests from different countries.
  • Regularly review your redirect rules to ensure they align with your business goals and user experience.

Conclusion

Cloudflare’s Redirect Rules make it easy to deliver location-specific experiences to your users. By setting up country-based redirects, you can ensure your visitors always land on the most relevant version of your site. Start using Cloudflare’s tools today to enhance your website’s performance and user engagement.

If you have any questions or need further assistance, feel free to leave a comment below!