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:
- Use some paid redirection service;
- Find an open source tool on Github, deploy it and manage redirections from there.
- Use any service we already paid.
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.
- Page Rules: You can define a page rule to trigger one or more actions whenever a certain URL pattern is matched.
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
- using * to match the pattern to be kept:
2. using $ and digits in the forwarded URL.
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:
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
- Cloudflare offers numerous helpful tools, with many of them included in the free plan.
- 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.
- Page Rules uses wildcard syntax for pattern matching and replacement.
- When redirecting, ensure you have a DNS record for the forwarding path; if not, create one in the DNS dashboard.