Cloudflare logo

Easy domain forwarding with Cloudflare Page Rules

All Cloudflare plans, also the free plan, includes a number of Page Rules. Pages Rules work at the Cloudflare proxy level and are used to customize settings and behavior of an entire domain, individual subdomains, or even specific paths of a domain (e.g., example.org/some/path/). Page Rules are powerful yet simple to set up and use.

In this post, I will get into how Page Rules can be used for forwarding one domain to another, e.g., forwarding https://www.example.com to https://www.example2.com. Typically, domain forwarding is handled either through the configuration of the webserver, a .htaccess file, or an HTML file. Handling domain forwarding through Cloudflare is an efficient and robust approach minimizing points of failure and complexity.

Step 0: Prerequisites

Before you can use Page Rules, you must add your domain to Cloudflare DNS, and the domain must be assigned the appropriate Cloudflare name servers at your domain registrar.

If you have already created DNS entries for the root domain and the www subdomain, you can continue with Step 2 or modify the settings to match those described in Step 1.

Step 1: Setting up DNS records

The first step is to set up DNS records. It is only possible to forward hostnames explicitly added as DNS records. Only the Enterprise plan allows wildcard DNS entries to be proxied through Cloudflare. In the following examples, I use the dummy root domain example.com and the subdomain www.example.com as the domain to forward.

  • Log in to the Cloudflare Dashboard.
  • Click on the domain in question, e.g., example.com.
  • Click the menu item “DNS”
  • Click “+Add record”

Fill in the following values for the different parameters for your domain:

  • Type: A
  • Name: @
  • IPv4 address: 192.0.2.1 This IP address is reserved as a dummy IP, so it will cause no IP conflicts.
  • TTL: Auto (default value.)
  • Proxy status: Make sure there is an orange cloud and the value ‘Proxied’
  • Click “Save”

Follow the same procedure for adding the www subdomain. Type in www instead of @ under the field “Name:”

Your DNS settings should look like this:

Step 2: Adding a Page Rule

After adding or modifying the DNS records, you will need to set up a Page Rule. You can forward a domain in a number of different ways depending on your specific needs. For simplicity, I have included two common setups, “Forward options one” and “Forward option two.”. Cloudflare will automatically forward both http and https requests, so no need to set up a rule for each protocol.

Forward option one

Forward option one forwards example.com to example2.com, including optional file or directory paths. This approach works well if you are changing your website’s domain and would like existing links at other sites to keep working. Who likes a 404?

Examples

example.com/
www.example.com/
forwards tohttps://www.example2.com/
example.com/some/path/
www.example.com/some/path/
forwards tohttps://www.example2.com/some/path/
example.com/myfile.html
www.example.com/myfile.html
forwards tohttps://www.example2.com/myfile.html

Add page rule

  • Click the menu item “Page Rules” after choosing the domain you want to forward from the Cloudflare Dashboard.
  • Click “Create Page Rule”
  • In the first field “If the URL matches:” type in *.example.com/*
  • In the dropdown menu under “Then the settings are:” choose “Forwarding URL”
  • In the dropdown menu “Select status code” choose “301 – Permanent redirect”
  • In the text field “Enter destination URL” type the domain you want to forward to including http:// or https://. Add “/$2” to the URL, e.g., https://www.example2.com/$2
  • Finally click “Save and Deploy”

Forward option two

Forward option two foreards all request to example.com to example2.com.

Examples

example.com/
www.example.com/
forwards tohttps://www.example2.com/
example.com/some/path/
www.example.com/some/path/
forwards tohttps://www.example2.com/
example.com/myfile.html
www.example.com/myfile.html
forwards tohttps://www.example2.com/

Add page rule

  • Click the menu item “Page Rules” after choosing the domain you want to forward from the Cloudflare Dashboard.
  • Click “Create Page Rule”
  • In the first field “If the URL matches:” type in *.example.com/*
  • In the dropdown menu under “Then the settings are:” choose “Forwarding URL”
  • In the dropdown menu “Select status code” choose “301 – Permanent redirect”
  • In the text field “Enter destination URL” type the domain you want to forward to including http:// or https://. E.g., https://www.example2.com/
  • Finally click “Save and Deploy”

Notice that both forwarding options will affect all defined subdomains that are proxied through Cloudflare. This is not always desirable. For example, if you have a DNS entry for the subdomain myapp.example.com and use this subdomain for an active site. In this case, you will have to either stop proxying the subdomain through Cloudflare or add two Pages Rules, one for the root domain and one for the www subdomain. E.g., setup a page rule for example.com/* and www.example.com/* instead of one rule for *.example.com/*

Leave a Reply

Your email address will not be published. Required fields are marked *

Do NOT follow this link or you will be banned from the site!