Steps to Forward the Root Domain to www Subdomain Using Cloudflare
-
Add your domain to Cloudflare: This is the first step to set up any kind of redirect. Make sure that your domain has a proxied DNS A or CNAME record that properly resolves DNS queries.
-
Create a DNS record for the www subdomain: This record can point to any IP address since all traffic will be redirected prior to reaching the address.
-
Set up a Page Rule for redirection: In the Cloudflare dashboard, navigate to the Page Rules section. Here, you will create a new rule for your root domain (example.com) to redirect to your www subdomain (www.example.com).
-
Enter the URL pattern: The pattern should match your root domain. For instance, you can use
*example.com/*
to match all paths under your root domain. -
Choose Forwarding URL: In the “Pick a Setting” dropdown, choose “Forwarding URL”.
-
Select status code: Choose 301 - Permanent Redirect or 302 - Temporary Redirect, depending on your needs. A 301 redirect is typically used for permanent URL changes, while a 302 redirect is used for temporary URL changes.
-
Enter the destination URL: This is the URL you want to redirect to, which in this case would be your www subdomain. To keep the path and query string from the original URL, use variables like
$1
and$2
that correspond to the wildcard parts of your URL pattern. For example, you could usewww.example.com/$1
as your destination URL. -
Save and Deploy Page Rule: Once you’ve set up the rule as desired, select “Save and Deploy Page Rule” to activate the redirect.
-
Remember, Page Rules require a proxied DNS record to work, and they will not apply to subdomains that do not exist in DNS or are not being directed to Cloudflare.