Cloudflare: How to Redirect One Domain to Another One?

approximately 2 minutes of reading

Cloudflare allows you to conveniently forward entire traffic from one domain to another without touching any servers at all. In this article I will show you how to achieve that with just a couple on entries in the dashboard.

This article assumes that you have 2 domains registered (one being the main, second being the alias) and defined already in your Cloudflare dashboard. The main one is configured in a traditional way so that the A record points to you server's static IP address and resolves the website. If this is the case, we can entirely focus on the alias domain as this the one where a redirection has to be defined.

For the purpose of this article, I'll use a real example case done for Jawor Parkiet company, a wooden floor manufacturer from Poland. Their main domain is jawor-parkiet.pl, their alias domain is jp.pl.

Desired effect

We want to redirect entire traffic from jp.pl to jawor-parkiet.pl with HTTP 301 permanent redirection.

Why 301 and not 302?

The 301 redirection is a permanent one as oppose to 302. It allows us to preserve any SEO value if applicable, also we don't expect this redirection behaviour to change anytime soon.

Solution

Since both domains are already up and running behind Cloudflare, all we have to do is to go the the alias domain's DNS section.

We have to create 2 DNS records here of type A.

Define the following DNS entires (both of type A):

  1. Type A, Name @, IPv4 address 192.0.2.1, Proxy status Proxied
  2. Type A, Name www, IPv4 address 192.0.2.1, Proxy status Proxied

Why 192.0.2.1? What is this IP address? What purpose it serves?

The 192.0.2.1 IP address is used by Cloudflare specifically for redirection. It doesn't route traffic to any servers and allows Cloudflare to apply redirection rule, we're about to specify in the next step.

Now, go to Rules and create new Redirection Rule

Rule definition is very trivial. Simply priovide the name of your rule so that you can easily identify it within the dashboard. For request matching select All incomming requests. For redirection type select Static from the dropdown list, as URL, provide the address of the destination domain (in my case https://jawor-parkiet.pl/), finally as Status code select 301 as this is a permanent type of redirection. Preserving query string in this case is not needed at all.

After saving the rule, the redirection starts working immediately, forwarding traffic from jp.pl to jawor-parkiet.pl.

We're done here.


Words: 406
Published in: Cloudflare

Related Articles   📚