GuidesJune 3, 20263 min read

How to redirect www to your apex domain (or vice versa)

Should visitors land on example.com or www.example.com? Pick one, redirect the other. Here's why it matters and how to set it up cleanly.

NBy Nxeon

example.com and www.example.com can serve the same site — but they shouldn't both be *the* address. Pick one as canonical and redirect the other to it. It's a small detail that keeps your links, analytics and SEO clean. Here's how and why.

Why you should pick one

If both versions load independently, you get avoidable problems:

  • Split SEO signals. Search engines may treat them as two sites, dividing ranking signals between them.
  • Messy analytics. Traffic splits across two hostnames, making reports harder to read.
  • Cookie and caching quirks. Sessions and caches keyed to one hostname don't apply to the other.

Redirecting one to the other means there's a single, canonical home for your site — cleaner in every way.

Apex vs www: which to choose?

There's no universally correct answer.

  • Apex (example.com) looks cleaner and is what people tend to type. The catch: the root of a domain can't use a CNAME, so pointing it at some services is slightly more constrained.
  • www (www.example.com) is a subdomain, so it can freely use a CNAME — handy if you front your site with a CDN or platform that expects one. Many large sites quietly redirect apex *to* www for exactly this reason.

Either is fine. Pick one, be consistent, and redirect the other.

Step 1: Make both names reach your server

First, DNS for both must point at your server. A typical setup:

Type: A
Name: @
Value: 203.0.113.10
TTL: 3600

Type: CNAME
Name: www
Value: example.com
TTL: 3600

Now both hostnames arrive at your server — the redirect itself happens *on the server*, not in DNS.

Step 2: Redirect at the web server

DNS gets visitors to the box; your web server decides the canonical name. In Nginx, redirect www to apex like this:

# Redirect www -> apex
server {
    listen 80;
    server_name www.example.com;
    return 301 https://example.com$request_uri;
}

# Serve the canonical apex
server {
    listen 80;
    server_name example.com;
    root /var/www/example;
}

The 301 is a *permanent* redirect, which is what you want — it tells browsers and search engines the move is official. $request_uri preserves the path, so www.example.com/pricing lands on example.com/pricing.

To go the other way (apex → www), just swap the hostnames in the two blocks.

Step 3: Do it over HTTPS

Redirects should point at the https:// version so visitors aren't bounced through insecure hops. Issue a certificate covering both names:

sudo certbot --nginx -d example.com -d www.example.com

Certbot will also wire up HTTP→HTTPS redirects, so a request to any variant ends up at the single canonical, secure URL.

Step 4: Verify

Check that the non-canonical name redirects with a 301:

curl -I https://www.example.com

Look for HTTP/… 301 and a Location: header pointing at your canonical URL.

The takeaway

One canonical hostname, a 301 redirect for the other, both served over HTTPS. It takes five minutes and quietly improves your SEO, analytics and link hygiene forever. With your domain and server together in Nxeon, you set the DNS records and the Nginx redirect in one place and never think about it again.

#dns#hosting#redirects#nginx#seo

Deploy your first server in under a minute

No credit card required to get started. Spin up a VPS, break things, and only pay for what you keep running.