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

URL: https://levelrail.com/learn/domains-https/redirect-www-to-the-bare-domain/
Updated: 2026-11-27

To redirect www to the bare domain, answer requests for `www.example.com` with a [301](https://levelrail.com/learn/glossary/#http-status-code)) 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](https://levelrail.com/learn/domains-https/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:

```text
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

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| `Could not resolve host: www.example.com` | No DNS record for www, or not spread yet | Add the record and check with `dig` |
| `NET::ERR_CERT_COMMON_NAME_INVALID` on www | Certificate does not list www | Reissue with both names, or let Caddy issue it |
| `ERR_TOO_MANY_REDIRECTS` | Bare domain redirects back to www, or two layers redirect | Keep one direction and one layer |
| Redirect lands on the home page, not the same path | Target written without the URI variable | Add `{uri}` or `$request_uri` |
| curl shows 301, browser shows old behavior | Browser cached an earlier redirect | Private window, clear cache, test with 302 first |
| `nginx: [emerg]` when reloading | Typo or missing certificate path | Run `sudo nginx -t` and read the line number |
| www shows the default welcome page | No server block matches that name | Add `server_name www.example.com` as above |
| `ERR_SSL_PROTOCOL_ERROR` on www | No TLS configuration for that name | Add the server block with a certificate for www |
| Redirect works for pages but not the homepage | Rule matches only paths beginning with `/x` | Redirect 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](https://levelrail.com/learn/domains-https/https-and-lets-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](https://levelrail.com/learn/domains-https/redirect-http-to-https-explained/)) so the plain HTTP version of www lands in one hop.

## Quick reference

| Task | Line |
| --- | --- |
| Caddy, permanent | `redir https://example.com{uri} permanent` |
| nginx, permanent | `return 301 https://example.com$request_uri;` |
| nginx, testing | `return 302 https://example.com$request_uri;` |
| Test config | `sudo nginx -t` |
| Reload | `sudo systemctl reload caddy` or `sudo nginx -s reload` |
| Check | `curl -I https://www.example.com/about` |

## Related lessons

- [DNS record types explained](https://levelrail.com/learn/domains-https/dns-record-types-explained/)): A and CNAME for the www name
- [HTTPS and Let's Encrypt](https://levelrail.com/learn/domains-https/https-and-lets-encrypt/)): a certificate that lists both names
- [Redirect HTTP to HTTPS, explained](https://levelrail.com/learn/domains-https/redirect-http-to-https-explained/)): the other redirect every site needs
- [Automatic HTTPS with Caddy](https://levelrail.com/learn/domains-https/automatic-https-with-caddy/)): Caddy issues the www certificate for you

## Next

Do the same for plain HTTP in [Redirect HTTP to HTTPS, explained](https://levelrail.com/learn/domains-https/redirect-http-to-https-explained/)).

## 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

- [Caddy docs, redir directive](https://caddyserver.com/docs/caddyfile/directives/redir)
