Redirect www.example.com to example.com using Single/Dynamic Redirects

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:

  1. Navigate to your website in Cloudflare, and then to DNS → Records. Magic link.
    image

  2. Add a new record. The type should be AAAA (IPv6 type), the name should be www (www subdomain), the IPv6 Address/value should be 100:: (IPv6 Discard, if nothing handles the traffic in Cloudflare, it won’t go anywhere), and Proxy Status should be on :orange:.


    Make sure to click Save.
    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.

  3. Navigate to Rules → Redirects → Create Rule. Magic link
    image

  4. 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 to Dynamic and add the following expression concat("https://example.com", http.request.uri.path), check Preserve Query String. Replace any mention of example.com with your own domain.


    Then click Deploy.

  5. 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.

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.