Background:
Redirect www.example.com
to example.com
(www subdomain to root/apex) using Single Redirects (also called Dynamic Redirects).
Looking for the opposite? Redirect example.com to www.example.com using Single/Dynamic Redirects
Prerequisites
-
Have an active and setup website in Cloudflare. Transfer your website to Cloudflare today, or use the Cloudflare Registrar to register a new domain.
-
I will be using
example.com
in place of your real domain. Anytime this used, please replace it with your own domain.
Guide:
-
Navigate to your website in Cloudflare, and then to DNS → Records. Magic link.
-
Add a new record. The type should be
AAAA
(IPv6 type), the name should bewww
(www subdomain), the IPv6 Address/value should be100::
(IPv6 Discard, if nothing handles the traffic in Cloudflare, it won’t go anywhere), and Proxy Status should be on .
Make sure to clickSave
.
This will allow traffic to flow into Cloudflare and hit your Redirect. Cloudflare will serve its own Proxy IPs at edge, so both IPv4 and IPv6 Clients (everyone) will be able to connect. -
Navigate to Rules → Redirects → Create Rule. Magic link
-
Create your Redirect Rule. Give it a name, like
www redirect to root
, and select Field:Hostname
Operator:equals
, Value:www.example.com
.
Under “Then… URL Redirect”, change Type toDynamic
and add the following expressionconcat("https://example.com", http.request.uri.path)
, checkPreserve Query String
. Replace any mention ofexample.com
with your own domain.
Then clickDeploy
. -
Wait a few minutes and test the redirect by visiting your site.
Troubleshooting:
-
Please note that due to DNS Cache, you may not be able to instantly connect and see the redirect. It may take an hour or so. Your Default DNS Resolver, usually operated by your Internet Service Provider, may cache for longer. Consider switching to Cloudflare’s 1.1.1.1 DNS Resolver today, for fast and privacy respecting DNS. Setup instructions: 1.1.1.1 — the Internet’s Fastest, Privacy-First DNS Resolver
This will manifest with errors such as “We’re having trouble finding this site” or ERR_NAME_NOT_RESOLVED or DNS_PROBE_FINISHED_NXDOMAIN. -
If you would like to provide any feedback on this tutorial, please post in the #Meta category, tag your post #TutorialFeedback.