DEV Community

Rickard Natt och Dag
Rickard Natt och Dag

Posted on • Originally published at willcodefor.beer on

1

Redirect www to non-www in Cloudflare

While setting up a new domain in Cloudflare I found that I wasn't handling www for the domains of this site. What I wanted was to redirect any requests to www to non-www, but it turned out to be harder than I expected. Mostly because I'm not great at DNS setups and I had a hard time finding any tutorials to help me.

I finally got it working by creating an A-record and a page rule.

Create a DNS record

If you don't have an A-record, I didn't, then create a dummy A-record in your domain's DNS. Set its name to www and the IPv4 address to 192.0.2.1. Make sure that it's proxied (the orange cloud). The page rule we are going to create will run before this record is resolved so the value shouldn't matter, but the page rule won't run if we don't have this record.

Page rule

  1. Go to page rules (Rules > Page Rules)
  2. Create a new rule. Set the first field, "if the URL matches", to www.mydomain.com/*. The asterisk will help us match any routes in the destination URL.
  3. Select "Forwarding URL" and "301 - Permanent Redirect"
  4. Set the destination URL to https://mydomain.com/$1

The asterisk combined with the $1 will match and keep any routes. For example, www.mydomain.com/sub/path will redirect to mydomain.com/sub/path. If we don't include this part we would be redirected to the root, i.e. www.mydomain.com/sub/path would redirect to mydomain.com.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay