Often it is needed to place a redirect from all pages in one domain to all pages (or a home page) in other domain and while the standard .htaccess file method on the server still works, for CloudFlare users there is a more efficient way to do that.
To redirect all pages from one domain to another one, do the following:
- Log into the CloudFlare and add the domain from which you want to redirect from. Choose the “Free” plan and skip the scanning of DNS records
- Go to the newly created domain profile and choose “DNS” -> “Records” section
- Add an A record for with a name of @ (at symbol) and point it to the IPv4 address: 192.0.2.1
- Add a CNAME record with a name of www and set the @ (at symbol) as a target
- Now switch to the “Rules” -> “Redirect Rules” section at the domain profile and press on “Create rule” button
- Set “Redirect all to mytargetdomain.tld” as the rule name, choose the conditional as “All Incoming Requests“
If you want to redirect all requests to the home page of the target domain:
7. Choose the “Type” in the statement as “Static” and set the “URL” as “https://mytargetdomain.tld” (the home url of your target domain)
8. Leave the “Status code” as 301 if you want to do a permanent redirect or change it to 302 if you want to do a temporary redirect
If you want to redirect all pages from one domain to all pages in another domain:
7. Choose the “Type” in the statement as “Dynamic” and add the following command in the “Expression” field (where https://mytargetdomain.tld is the root/home URL of your domain):
concat("https://mytargetdomain.tld", http.request.uri.path)
8. Leave the “Status code” as 301 if you want to do a permanent redirect or change it to 302 if you want to do a temporary redirect
9. If you want to redirect also the query strings (e.g. ?myvar=1&myvar=2) tick the checkbox “Preserve query string“
10. Save the newly created redirect rule