How to redirect domain according to country

Last Modified

Why do I need to redirect my domain name based on country?

There are so many application scenarios like this, take the requirement we encountered as an example

Since Cloudflare does not provide services in China, which makes it slow for users in China to access your website, if you want to do business in China, then our recommendation is to set up your website in China.

What do I need to pay attention to when setting up a website in China?

1.Domain name ICP Licensing
2.Selection of cloud server
3.Necessary contact information

Domain name configuration

A common example of a multilingual website is zhuli.eu, where the URL is differentiated according to the type of language:

1. English https://zhuli.eu/en/
2. French https://zhuli.eu/fr/
3. Simplified Chinese https://zhuli.eu/zh-CN/
4. Traditional Chinese https://zhuli.eu/zh-TW/

So you may notice: all the URLs use the same domain name zhuli.eu, and in order to start the service in China, we need to add a second-level domain name such as zh.zhuli.eu to point to the IP of the cloud server in China, so that when a Chinese user opens zhuli.eu, they should automatically be redirected to zh.zhuli.eu.

Functionality Implementation

OK, if you're ready, let's get down to business and show you how to do this with cloudflare Page Rules.

Open the backend and find Transform Rules and add a rule

CloudFlare Transform Rules

Here I have set China, Taiwan and Hong Kong to be included in the rule.

In the Rewrite to setting, We choose Dynamic, and fill in concat("/anythingyouwant", http.request.uri.path)

Click "Deploy", and when you're done, we'll go ahead and add the Page Rules.

add the Page Rules

Fill in the parameters in concat("/anythingyouwant", http.request.uri.path) in the URL settings.

www.zhuli.eu/anythingyouwangt/*

The Enter destination URL is added to the destination URL

zh.zhuli.eu/$1

Finally we click "Save and Deploy Page Rule"

Note that if you have more than one rule, you can prioritize them on the Page Rules admin page.