Redirect One Domain to Another Via Cloudflare

Redirect One Domain to Another Via Cloudflare
Photo by Ravi Chandra / Unsplash

Recently, we managed to resolve a redirection issue involving two sites. Site A contains historical data, while Site B has a domain name. Our goal is to direct URLs from Site B to the content on Site A while minimizing effort.

We had three options:

  1. Use some paid redirection service;
  2. Find an open source tool on Github, deploy it and manage redirections from there.
  3. Use any service we already paid.

Cloudflare

Fortunately, we discovered that Cloudflare offers two straightforward redirection methods, and we have already paid for its use. Thus, choosing Cloudflare was an obvious decision for us.

Within Cloudflare, there are two tools that provide this functionality:

Redirects: Single Redirects: Allow you to create static or dynamic redirects at the zone level. Dynamic redirects are advanced URL redirects, such as redirects based on the source country of requests. Bulk Redirects: Allow you to define a large number of redirects at the account level. These URL redirects are essentially static — they do not support string replacement operations or regular expressions.

  1. Page Rules: You can define a page rule to trigger one or more actions whenever a certain URL pattern is matched.

Page Rules

As we needed to redirect multiple URLs based on specific criteria, we opted for Page Rules to accomplish the task.

Page Rules on Cloudflare uses wildcard instead of regular expression to match and replace pattens. According to the document, the syntax of wildcard looks like this

  1. using * to match the pattern to be kept:

www.abc.com/ex-*

2. using $ and digits in the forwarded URL.

www.yxz.com/ex-$1

DNS setting

Additionally, if you don't have a DNS record for the forwarding path, you need to create one in the DNS dashboard. For instance, if you have an original URL like this:

blog.abc.com/ex-* and

and don't have blog.yxz.com in the new domain, you need add a DNS record for blog.yxz.com first on the DNS dashboard. Only until the  DNS record being created, can you successfully redirect blog.abc.com to blog.yxz.com.

Takeaway

  1. Cloudflare offers numerous helpful tools, with many of them included in the free plan.
  2. Becoming familiar with a specific Cloudflare feature can take time. ChatGPT serves as a useful tool for gaining a quick overview. While some information might be outdated, it's still a good starting point.
  3. Page Rules uses wildcard syntax for pattern matching and replacement.
  4. When redirecting, ensure you have a DNS record for the forwarding path; if not, create one in the DNS dashboard.