Have you surfed the web lately? That would be the world wide web and origin of the “www” you often see in the URL of your web browser. A few decades ago it was the norm but slowly new conventions arise and people’s expectations change.
Today websites with and without the “www” are common considering they almost always respond with the same content. One is synonymous with the other but there is usually an obscured name preference. The difference comes down to the convention adopted by content providers. Web browsers have gotten in on the act too and enforce their own standard such as Chrome’s 2018 decision to strip www from the browser’s address bar.
Technically not the Same
At the lowest levels slightly different configurations are needed and as you would expect DNS is at fault. Standards limit how a zone apex can be defined in zone files. It can point to an IP address but not another DNS name. A typical “www” has no such limitation. Using an IP address or name affects backend tooling who’s job it is to distribute content from the source. For example a DNS name could utilize a DNS load balancer and content nodes spread around the world. Clients could resolve the website to an IP address for the content closest content node. But if an IP address is used, all clients will be sent to the same place. Even if not optimal either should work and be transparent to the end user.
Preference of Major Sites
Fortunately today those design patterns are well know. Let’s take a quick look to see what name preferences some prominent websites use today. We can poke a few major sites to get an idea what they prefer. Using curl
to compare the https redirect location field is a simple way.
This first group provide redirects to a “www” subdomain for the website.
% curl -Is http://microsoft.com | grep Location
Location: https://www.microsoft.com/
% curl -Is http://google.com | grep Location
Location: http://www.google.com/
This second group omits “www” in their redirect.
% curl -Is http://ibm.com | grep Location
Location: https://ibm.com/
% curl -Is http://unc.edu | grep Location
Location: https://unc.edu/
Enforcing a Standard
For my personal site, I want to keep it old school and rely on “www” as my main web presence. But the next two sections will look at doing it both ways with Cloudflare DNS and Cloudflare Pages. Other products no doubt will be similar.
Setup 1 - Prefer “www”
In this first case we will use “www”. We can define a few behaviors and build a matrix to test with curl
.
- Redirect port 80 (HTTP) to port 443 (HTTPS)
- Redirect any domain HTTP call to the “www” subdomain
Request | Response Code | Redirect Location |
---|---|---|
http://defingo.net | HTTP 301 | https://www.defingo.net/ |
http://www.defingo.net | HTTP 301 | https://www.defingo.net/ |
https://defingo.net | HTTP 301 | https://www.defingo.net/ |
https://www.defingo.net | HTTP 200 |
Additionally let’s add something to preserve any legacy content links that may exist. This occurs often with old printed material and is often a reason the www/non-www convention cannot be changed easily.
- Preserve the full path of the URL in the redirect
Request | Response Code | Redirect Location |
---|---|---|
http://defingo.net/blogs | HTTP 301 | https://www.defingo.net/blogs |
http://www.defingo.net/blogs | HTTP 301 | https://www.defingo.net/blogs |
https://defingo.net/blogs | HTTP 301 | https://www.defingo.net/blogs |
https://www.defingo.net/blogs | HTTP 200 |
This is easily accomplished via two sections of the Cloudflare Dashboard.
Page Custom Domain
Cloudflare Pages needs to know to answer for both the domain and the www so two custom domains are needed. Navigate to the Workers & Pages section for your content and add them.
- defingo.net
- www.defingo.net
Page Rule
Page rules handle the redirections. You may already have settings enabled to redirect all HTTP to HTTPS. Under the Page Rule for the domain define the following with my example domain.
- URL “defingo.net/*”
- Setting Forwarding URL
- Status Code 301 Permanent Redirect
- Destination URL https://www.defingo.net/$1
Of note is the destination url. It includes the ‘https’ prefix and the variable $1 to match the original URL path.
Setup 2 - Prefer non-WWW
This second case will prefer not using “www” for content. The simplest method just skips the step of adding a “www” subdomain in DNS, but you can keep a redirect with a few more rules. This may create a redirect chain.
Request | Response Code | Redirect Location |
---|---|---|
http://defingo.net | HTTP 301 | https://defingo.net/ |
http://www.defingo.net | HTTP 301 | https://www.defingo.net/ |
https://defingo.net | HTTP 200 | |
https://www.defingo.net | HTTP 301 | https://defingo.net |
Page Custom Domain
You would define the same two custom domains as above.
Page Rule
The first catches all HTTP traffic and redirects to the base domain with HTTPS.
- URL “http://*”
- Target URL https://${1}
The second catches traffic to “www” and redirects to the base domain also with HTTPS.
- URL “https://www.*”
- Target URL https://${1}
In the End
It just comes down to picking a name convention and being consistent. Either should work and usually probably won’t even notice if the web browser is going to hide the “www” anyway.