Chapter 3 · Lesson 4 of 22
Domains and HTTPS

Redirect www to the bare domain (non-www redirect)

A www redirect sends every request for www.example.com to example.com with a 301 status, so each page has one address.

By the end you will

Send www.example.com to example.com with a 301 redirect and verify it with curl.

Time
15 min
Level
Beginner
Check
4 questions
You need
  • A domain already served over HTTPS by Caddy or nginx

To redirect www to the bare domain, answer requests for www.example.com with a 301 and a Location header of https://example.com plus the original path. The www name must resolve to your server and have a certificate, or visitors never reach the redirect.

How it works#

DNS names are independent. example.com and www.example.com are two different names, each with its own records, and a server can answer for either, both or neither. A redirect is an HTTP response that tells the client “ask somewhere else”: a status code (301 for permanent) and a Location header with the new URL. The browser follows it automatically and shows the new address.

For a redirect to run at all, three things must be true. The www name must resolve to your server (DNS). The server must complete a TLS handshake for that name, which needs a certificate that includes www.example.com, because TLS happens before any HTTP response. And a server rule must answer for that host with the redirect. If any of the three is missing, the visitor sees a DNS error, a certificate warning, or the wrong site.

Which direction you choose does not matter technically. Pick one (this lesson uses the bare domain), redirect the other to it, and keep the rule in one direction only.

Step 1: Add a DNS record for www#

A redirect can only run if www reaches your server. Add an A record (or a CNAME to the bare domain) for www. See DNS record types explained. What you should see from dig +short www.example.com: your server’s IP, or example.com. followed by the IP when a CNAME is used.

Step 2: Add the redirect#

With Caddy, put this in your Caddyfile:

output
www.example.com {
    redir https://example.com{uri} permanent
}

{uri} keeps the path and query string. Caddy gets a certificate for the www name itself.

With nginx, add a server block. It needs a certificate that covers www.example.com:

nginx
server {
    listen 443 ssl;
    server_name www.example.com;
    # ssl_certificate and ssl_certificate_key go here
    return 301 https://example.com$request_uri;
}

$request_uri is the original path plus query string. Reload the server afterwards (sudo systemctl reload caddy or sudo nginx -s reload). Before you reload nginx, run sudo nginx -t. What you should see: a message that the syntax is OK and the test is successful.

Why try 302 first#

A 301 tells browsers and search engines the move is permanent, and browsers cache it hard. If the target is wrong, it can stick for visitors even after you fix the server. While you test, use a temporary redirect (return 302 ... in nginx, or drop permanent in Caddy, which then sends a temporary one). Switch to 301 when you are sure. If you also serve plain HTTP for www, let it redirect straight to https://example.com so visitors do not bounce twice.

Check it worked#

bash
curl -I https://www.example.com/about?x=1

-I fetches only the headers. You should see a 301 status line (HTTP/2 301 or HTTP/1.1 301) and a header like location: https://example.com/about?x=1. The path and query are kept because of {uri} or $request_uri. Then curl -I https://example.com/about should show a normal 200, not another redirect.

Edge cases and gotchas#

  • The browser cached an old 301. After a wrong permanent redirect, curl shows the right answer but the browser does not. Use a private window or clear the site data, and test with 302 first next time.
  • Certificate must include www. TLS runs before the redirect. A certificate for only example.com gives NET::ERR_CERT_COMMON_NAME_INVALID on www, and the visitor never sees your redirect.
  • Both directions redirect. If an app or CDN setting forces www while your server forces the bare domain, you get ERR_TOO_MANY_REDIRECTS. Find every layer that redirects and keep one rule.
  • CNAME at the apex is not allowed. Make www the CNAME and keep A records on the bare domain, not the other way round.
  • Behind a CDN or proxy. The CDN may already offer a redirect rule. Use one place, not both.
  • Cookies. Cookies set on www.example.com are not sent to example.com unless the cookie’s domain was set to cover both. After the move, visitors may need to log in again.
  • HTTP on www. If port 80 for www has no rule, http://www.example.com may reach the wrong site or fail. Add a plain HTTP rule too.

Troubleshooting#

SymptomLikely causeFix
Could not resolve host: www.example.comNo DNS record for www, or not spread yetAdd the record and check with dig
NET::ERR_CERT_COMMON_NAME_INVALID on wwwCertificate does not list wwwReissue with both names, or let Caddy issue it
ERR_TOO_MANY_REDIRECTSBare domain redirects back to www, or two layers redirectKeep one direction and one layer
Redirect lands on the home page, not the same pathTarget written without the URI variableAdd {uri} or $request_uri
curl shows 301, browser shows old behaviorBrowser cached an earlier redirectPrivate window, clear cache, test with 302 first
nginx: [emerg] when reloadingTypo or missing certificate pathRun sudo nginx -t and read the line number
www shows the default welcome pageNo server block matches that nameAdd server_name www.example.com as above
ERR_SSL_PROTOCOL_ERROR on wwwNo TLS configuration for that nameAdd the server block with a certificate for www
Redirect works for pages but not the homepageRule matches only paths beginning with /xRedirect the whole host, not a path pattern

Real-world scenario#

Jo’s blog answers on both example.com and www.example.com. Search results show both, and her login on one name does not work on the other. She decides the bare domain is canonical. Her DNS has an A record for @ only, so the first test fails.

bash
dig +short www.example.com
curl -I https://www.example.com/about

dig prints nothing, so she adds an A record for www. Then curl complains about the certificate, because her certificate lists only example.com. She reissues it with both names (see HTTPS and Let’s Encrypt), adds the nginx block, runs sudo nginx -t and reloads. She tests with return 302 first. Once curl -I shows location: https://example.com/about, she switches to 301. A week later she adds HTTP to HTTPS redirects so the plain HTTP version of www lands in one hop.

Quick reference#

TaskLine
Caddy, permanentredir https://example.com{uri} permanent
nginx, permanentreturn 301 https://example.com$request_uri;
nginx, testingreturn 302 https://example.com$request_uri;
Test configsudo nginx -t
Reloadsudo systemctl reload caddy or sudo nginx -s reload
Checkcurl -I https://www.example.com/about

Next#

Do the same for plain HTTP in Redirect HTTP to HTTPS, explained.

Check what you learned4 questions
1Why does a www redirect need its own DNS record?
2Which curl result shows the redirect works for https://www.example.com/about?
3What causes a redirect loop between the two names?
4You test a 301 to the wrong target, fix the config, but your browser keeps going to the wrong place. Why?

Frequently asked questions

Why redirect www at all?

Without it, the same page lives at two addresses. Visitors and search engines see duplicates, and cookies can differ between the two names.

Should I redirect to www instead?

You can. Either choice works. Pick one, redirect the other to it, and keep it that way.

Do I need a certificate for www if I only redirect?

Yes. The browser completes the TLS handshake before it sees the redirect, so the www name must be on a certificate or visitors get a certificate error.

Will the redirect keep the page path and query string?

Only if you write it to. The examples use the original URI variable so /about?x=1 lands on the same path on the bare domain.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios