1. Omid Sayfun
  2. /
  3. Notebook
  • Home
  • About
  • Notebook
  • Token Usage
  • Whisper Usage
Tools
  • Agent Knowledge Base

Redirect www subdomain with Cloudflare

June 17, 2024 · Updated on August 09, 2026

I wanted to run Google Ads for one of my side projects, but I noticed a problem: Google was displaying my domain as www.example.com, even though I hadn’t set up a www subdomain. This meant that if anyone tried to visit www.example.com, they’d find nothing. Since I’m not using a reverse proxy and rely on Cloudflare (CF) rules to direct traffic, I figured the best solution was to set up the www subdomain and redirect www to non-www on the Cloudflare side. It takes two things: a DNS record for www, and a rule that forwards it to the root domain.

Add a www DNS record and keep it proxied

  1. Log in to Cloudflare: First, I logged in to my Cloudflare account.
  2. Add the www Subdomain:
    • Navigate to the DNS settings.
    • Add a new DNS record with the following details:
      • Type: CNAME or A (depending on your setup)
      • Name: www
      • Target/Value: Your application’s IP address or main domain
    • Proxy Status: Ensure that the proxy is enabled (orange cloud icon). If the proxy is not enabled, Cloudflare won’t be able to manage the traffic.

If you leave the record on DNS only (grey cloud), Cloudflare never sees the request, and the www redirect will not work.

Forward www to the root domain with a page rule

  1. Navigate to Page Rules:
    • Go to the “Rules” section in Cloudflare.
    • Click on “Page Rules”.
    • Create a new page rule.
  2. Set Up the Redirect:
    • URL Pattern: Set the URL pattern to www.example.com/*. This pattern catches all URLs starting with www.example.com.
    • Settings: Choose “Forwarding URL” from the drop-down menu.
    • Status Code: Select either a 301 (permanent) or 302 (temporary) redirect.
    • Destination URL: Enter https://example.com/$1. The $1 ensures that any subdirectory path from the original URL is appended to the new URL.

Cloudflare also offers Redirect Rules in the same “Rules” section. They take the same source pattern and destination, so use them if your account no longer shows Page Rules.

The rule works in both directions. To redirect the root domain to www instead, set the pattern to example.com/* and the destination to https://www.example.com/$1.

Any request to www.example.com now redirects to example.com, and subdirectory paths are preserved.

Join My Newsletter

Occasional notes on software, tools, and things I learn. No spam.

Unsubscribe anytime.

Continue Reading

  • Embeddings rot too. Running pgvector in production.Aug 28, 2026
  • Stop shipping retrieval changes on vibesAug 25, 2026
  • Your RAG is confidently wrong without hybrid searchAug 21, 2026
  • Stop tuning everything. pgvector has three knobs that matter.Aug 18, 2026
  • Your agent's knowledge base is lying to you. Run these 14 checks.Aug 18, 2026