Using Cloudflare redirect rules
Redirect rules, like redirecting http to https, are done by the web server
On Cloudflare, which is does not use Nginx as a web server, but a custom build one, follow these steps:
Redirecting to the root domain To make your root domain (e.g., example.com) redirect to your www subdomain (e.g., www.example.com), you need to set up a forwarding rule within your Cloudflare dashboard. This ensures that anyone who types the root domain is automatically sent to the www version, which is considered a best practice for consistency and SEO.
Step 1: Configure Your DNS Records
- Before setting up the redirect, you must have a DNS record for the root domain that is proxied through Cloudflare. This allows Cloudflare to intercept and handle the redirect.
- In your Cloudflare dashboard, go to the DNS app.
- Create an A record for your root domain. Use @ as the name.
- For the IPv4 address, you can use a placeholder IP like 192.0.2.1. This is a reserved IP that won’t route traffic to an actual server but will allow Cloudflare’s rules to apply.
- Make sure the Proxy status is set to Proxied (the orange cloud icon should be enabled).
Step 2: Create a Redirect Rule
- Redirect rules are the modern and recommended way to manage redirects in Cloudflare.
- In your Cloudflare dashboard, go to Rules > Redirect Rules.
- Click Create rule and give it a descriptive name like “Redirect root to www”.
- Under the When incoming requests match section, set up a rule expression.
- Choose Hostname as the field.
- Set the operator to equals.
- Enter your root domain (e.g., example.com) as the value.
- If you are matching all traffic, including HTTP and HTTPS, you can use the expression: (http.host eq “example.com”).