How to redirect a Cloudflare domain to another URL

Every time I find myself trying to re-learn something I'm taking the time to document. Here's one simple example:

I'm a fan of Cloudflare domains because they are at cost purchase and renewal and don't have much of the spam and upselling comm with many other domain providers (like those famous for Superbowl commercials).

However, sometimes doing something in Cloudflare as simple as redirecting one domain to another static URL can be more complicated than you'd expect.

I've spent enough time relearning how to do a simple static redirect (instead of Cloudflare's more complicated dynamic version) by digging through Cloudflare docs and forum posts that it was worth codifying how to do the process to save myself time in the future.

I hope this saves you some time too if you found this searching for a simple solution!

How to redirect a domain A to domain B in Cloudflare:

Instructions

1) Login to your Cloudflare dashboard.

2) Select the domain you want to redirect.

3) Choose Records →DNS on the left-hand menu.

4) Create two new A DNS records:

Type

Name

IP Address / Content

Proxy Status

TTL

Notes

A

@

192.0.2.0

Proxied

Auto

Sets your main domain (e.g. example.com)

A

www

192.0.2.0

Proxied

Auto

Sets www subdomain (e.g. www.example.com)

This creates two A records that point to an IPv4 reserved address set by the IETF for examples. Without the proxy, the DNS records should route to nowhere. However, we'll use the proxying to have Cloudflare redirect the DNS request before it can reach these.

Here's an example of setting the records:

5) Choose Rules → Templates on the left-hand menu.

6) Select "Redirect to a Different Domain:"

7) Configure the following settings:

  • All incoming requests.
  • URL redirect → Type: Static.
  • URL redirect → URL: The page you want to redirect to including protocol.
  • URL redirect → Status Code: Usually 302 = Temporarily unavailable or 301 = Permanently moved. You can learn more about the nuances of redirect codes on the MDN Docs.
  • Place at → Select Order: First (unless you have other rules configured).

8) Click Save

9) Test your redirect by going to your domain. It may take a while for the DNS change to propagate. See Troubleshooting below if you are getting "This site can't be reached" DNS_PROBE_FINISHED_NXDOMAIN or other issues.

Troubleshooting

  • If you're getting "This site can't be reached" DNS_PROBE_FINISHED_NXDOMAIN issues you can try the following to figure out if you're hitting a stale DNS cache which will usually resolve in a few hours to a day max:
    • If a DNS checker website is showing that the correct A record is going through Cloudflare's proxies but you're getting a DNS issue, it's most certainly a DNS cache issue.
    • When in doubt, try using another browser.
    • Try accessing the URL on your phone through LTE or another network (this bypasses your current network's DNS cache).
    • Try clearing your local DNS cache:
      • Mac: sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
      • Windows: ipconfig /flushdns
    • Try setting your computer's DNS settings to use Cloudflare's 1.1.1.1 service. Instructions for Mac and Windows.
  • When in doubt, try using another browser.
  • If you use a standard online DNS checker and the A record is pointing to Cloudflare's proxies, but you're still getting "This site can't be reached," it is highly likely that it is a DNS cache issue above.

Notes

  • There are several reserved IPv4 addresses that intentionally don't route to servers, so for some other examples you might see IP addresses like 192.0.2.1 or 192.0.2.2 in addition to 192.0.2.0. These are addresses in TEST-NET-1. See this Stackoverflow post and this IETF RFC 5737 for more details.

Read more