Learn how to redirect domain using Cloudflare in this complete beginner’s guide to Cloudflare redirects.
Are you looking to redirect the domain that you have added to your Cloudflare account? Or have you purchased a domain name with Cloudflare and want to redirect the domain to another website?
Cloudflare offers fast and powerful redirection features, including everything from simple redirect features to complex bulk and dynamic redirects.
Whether you are looking to move to an entirely new domain or want to redirect a few URLs on your existing site, the Cloudflare redirects feature or Cloudflare domain forwarding can be really useful.
You can easily set up domain redirects using Cloudflare by following the steps in this quick and easy guide.
To use the Cloudflare redirection feature, first, you need to ensure the domain name is already added to your websites under the Cloudflare account.
For domains registered using other domain registrars, you can click on the ‘+Add a site‘ button and start the process to point your domain to Cloudflare servers.
If you have registered the domain on Cloudflare, it’s automatically added to the list of your websites.
Also, if you don’t have any DNS records for the domain, you will also need to add two proxied records for the redirection to work. I recommend creating two A records (@) and (www) pointing to 126.96.36.199 – see detailed instructions in the below section.
Steps to Redirect Domain Using Cloudflare
Once the domain is on your Cloudflare account, you will need to use the Page Rules feature of Cloudflare to implement page redirections.
Follow the steps below to setup domain redirects using Cloudflare Page Rules:
Step 1: Login to your Cloudflare account and select the domain name that you want to redirect.
Step 2: Scroll to the “Rules” tab on the sidebar for the domain and select the “Page Rules” option.
Step 3: On the Page Rules page, create a new Page Rule by selecting the “Create Page Rule” button.
Step 4: Configure the following settings for redirecting your domain;
1. In the first step, enter the URL of the domain that you want to redirect. For a complete domain redirect, enter your domain name here;
I have entered my domain name, “guidesandinfo.com”, in the example above.
If you are looking to redirect a specific page, enter the URL of the page that you want to redirect in the “If the URL matches” field. For example, if you want to redirect the about page URL, you would enter “/about”.
2. Select the “Forwarding URL” option from the “Then the settings are:” dropdown menu.
3. Select the “Status Code” option from the “Choose Status Code” dropdown. The status code determines how the browser will handle the redirect. For most cases, you will want to choose the “301 – Permanent Redirect” option.
4. Enter the URL of the new domain that you want to redirect to in the “Enter destination URL” field as follows;
I have used my website domain name “https://webguided.com” in the example above.
Step 5. Save the Page Rule by selecting the “Save and Deploy Page Rule” button.
Now, whenever someone visits the domain name that you specified in the “If the URL matches” field, they will be redirected to the new domain that you specified in the “Redirect URL” field.
Summary: If you just want a single screenshot for reference on how to enter the URLs for all possible redirections of different versions of your old domain URL to a new domain, see this;
This redirects all versions of your old website (with or without www or with https or http) to the new domain.
*guidesandinfo.com/* – Set forwarding URL to – https://webguided.com/$2
For the above redirection example in the screenshot, all these redirections will work after you save and implement the rule.
guidesandinfo.com/ 301 redirects to https://webguided.com www.guidesandinfo.com/ 301 redirects to https://webguided.com guidesandinfo.com/example 301 redirects to https://webguided.com/example www.guidesandinfo.com/example 301 redirects to https://webguided.com/example
If you want to further understand the use of (*) character and $X in the above example, see the notes below.
Wildcard matching and referencing
You can use wildcard matching and referencing to create powerful redirection rules in Cloudflare.
To match every page on a domain that you are redirecting, you need to enter as follows.
In the example above, I have used an asterisk (*) character to create a dynamic pattern that matches all URLs in the domain that I am looking to redirect.
Referencing wildcard matches
You can later reference a matched wildcard using the $X syntax, where X stands for the index of a glob pattern. In other words, $1 represents the first wildcard match, $2 represents the second one, and so forth.
In our example above, the /$1 at the end of the new domain URL enables the wildcard redirection to the corresponding URLs of the new domain.
The above single redirection rule example will help you achieve all the following redirects;
http://guidesandinfo.com/ 301 redirects to https://webguided.com https://guidesandinfo.com/ 301 redirects to https://webguided.com http://guidesandinfo.com/* 301 redirects to https://webguided.com/* https://guidesandinfo.com/* 301 redirects to https://webguided.com/*
If you use *example.com/* , note that the URL path should be referenced by the second wildcard match ($2) as the first one is referencing different versions of your old domain (with or without www or with https or http).
*example.com/* – Set forwarding URL to – https://yournewdomain.com/$2
Please note that you can create only a limited number of Page Rules as per your Cloudflare plan and the Free plan allows for only three Page Rules. But as you can see in the examples above, you can create dynamic patterns for multiple redirects with a single-page rule.
If you need to redirect multiple pages, you will need to create a separate Page Rule for each page or create dynamic pattern redirects within your limit.
Cloudflare’s Page Rules feature is a powerful tool that can be used to do much more than just redirect domains.
To learn more about all the different things that you can do with Page Rules, check out the Cloudflare Page Rules documentation.
Adding Proxied DNS Records for Redirection
For the domain that you want to redirect, you will need to make sure there are proxied records for the root domain for the redirect to work.
I recommend creating two A records (@) and (www), both pointing to a dummy IP address. Don’t worry about the value, as the forwarding page rule will execute before the target IP.
You just need to make sure these DNS records are proxied through Cloudflare – “Orange cloud” enabled.
|A||@||192.0.2.1||Automatic||Proxied (Orange cloud)|
|A||www||192.0.2.1||Automatic||Proxied (Orange cloud)|
Go to the DNS tab for the domain that you want to redirect and add the records under the DNS management section. You can delete all other records in this section.
Make sure both the records are proxied (orange cloud icon). The “@” will change to your root domain name once you save the changes.
Other Domain Redirects Settings using Cloudflare
While the above configuration will redirect all your traffic from one domain to another domain, there might be specific cases of redirection that you might be looking to achieve.
Let’s look at the redirection settings for other specific use cases.
Redirect non-www to www version
If you are looking to redirect the non-www to www version of a domain, you will need to enter the details in the below format;
I have used the domain name ‘example.com’ in the example above.
Redirect www to non-www version
Similarly, if you are looking to redirect the www version of a domain to a non-www version, you will need to enter the domain details in the below format;
I have used the same domain name, ‘example.com’ in the example above.
Redirect Specific URL on your Domain to another URL
What if you want to redirect a specific URL or a path on your domain to another specific URL? Let’s say you want to redirect a specific URL, such as /google on your domain, to Google’s website.
This is what you would enter in the redirection settings.
In the example above, ‘example.com’ would be your site ‘/google’ is the specific path that you want to redirect.
Redirect Domain Registered on Cloudflare?
Cloudflare also acts as a domain registrar and allows you to register domain names. The process to redirect the domain names purchased on Cloudflare is still the same.
The website for the domain registered on Cloudflare is already added to your account. You don’t have to take any additional steps pointing and adding the domain to your Cloudflare account.
For redirecting the domain, navigate to your Domain Setting and implement the Page Rules as described in the steps above.
Make sure you also add the proxied DNS records with dummy IP addresses as suggested in the above section (there are no records created by default when you just register the domain).
As Cloudflare has recently started offering domain registration services, you can directly purchase the domain name on Cloudflare and save the hassle of pointing it out from another registrar. The domain gets directly added to the list of your websites.
Redirections using Cloudflare Workers?
Cloudflare Workers is another powerful feature that you can use to implement redirections for your domain.
Adding redirections using Cloudflare workers is an advanced option, and you will need to be comfortable writing code to implement redirections.
You can create Cloudflare workers by navigating to the Workers tab → Manage Workers → Create a Worker.
Cloudflare Workers is actually useful if you have complex cases of redirection, and for most cases, the Page rules feature should be adequate.
We hope this guide helped you learn how to redirect your domain with Cloudflare. Check out our more website guides and tutorials.