Redirect Multiple WWW Domains to One Non-WWW Domain in Nginx
This guide shows you how to configure Nginx to redirect all traffic from multiple www subdomains (and optionally other domains) to a single non-www domain. By the end, any request to www.example.com, www.example.net, or www.example.org will permanently redirect to https://example.com, preserving the path and query string. This consolidates your site’s authority, avoids duplicate content, and ensures a consistent user experience.
What You’ll Need
- Root or sudo access to your server.
- Nginx installed and running (tested on Nginx 1.18+ but works on most versions).
- All domains (www and non-www) must be configured to point to your server’s IP via DNS (A/AAAA records).
- An existing server block (virtual host) for your primary non-www domain (e.g.,
example.com). - Optional: SSL certificates for each domain (if you want HTTPS redirects).
Step-by-Step
- Identify your primary domain and the list of www domains. For this guide, we’ll use
example.comas the target non-www domain, and we’ll redirectwww.example.com,www.example.net, andwww.example.orgto it. Replace these with your actual domains. - Open your Nginx configuration. The main config is usually at
/etc/nginx/nginx.conf, but you’ll likely want to add a new server block in a separate file under/etc/nginx/sites-available/(Debian/Ubuntu) or/etc/nginx/conf.d/(CentOS/RHEL). Create a new file, e.g.,/etc/nginx/sites-available/redirect-www. - Create a server block for the www redirects. This block will listen on port 80 (HTTP) and catch all requests to the www domains. Use the
server_namedirective to list all the www domains you want to redirect. Inside, use areturn 301to permanently redirect to your primary domain, preserving the request URI and query string. - Write the redirect server block. Here’s a minimal example:
server { listen 80; server_name www.example.com www.example.net www.example.org; return 301 https://example.com$request_uri; }This redirects all HTTP requests for those www domains to
https://example.comwith the same path. If you don’t have SSL yet, you can redirect tohttp://example.com$request_uriinstead. - Add HTTPS redirects (if you have SSL certificates). If you want to also redirect HTTPS requests to the www domains, create a second server block listening on port 443 with SSL. You’ll need certificates for each www domain (or a wildcard certificate). Example:
server { listen 443 ssl; server_name www.example.com www.example.net www.example.org; ssl_certificate /etc/nginx/ssl/www.example.com.crt; ssl_certificate_key /etc/nginx/ssl/www.example.com.key; return 301 https://example.com$request_uri; }Note: If you have separate certificates for each domain, you’ll need separate server blocks or use SNI with a single block and multiple
ssl_certificatedirectives (Nginx 1.15.10+ supports multiple certificates withssl_certificateandssl_certificate_keyfor each). For simplicity, a wildcard certificate covering all www domains is easiest. - Ensure your primary domain server block exists. You must have a server block for
example.comthat listens on port 80 and 443 (if using HTTPS) and serves your site. If you don’t have one, create it. For example, a basic HTTP block:server { listen 80; server_name example.com; # ... your site configuration ... }If you’re using HTTPS, add a 443 block with SSL and your site config.
- Test the Nginx configuration. Run
sudo nginx -tto check for syntax errors. If it reports success, you can proceed. - Reload Nginx. Apply the changes with
sudo systemctl reload nginx(orsudo service nginx reloadon older systems). The redirects are now active. - Verify the redirects. Use
curlor a browser to test. For example,curl -I http://www.example.com/some/pathshould return a301 Moved Permanentlyheader withLocation: https://example.com/some/path. Test each www domain and both HTTP and HTTPS if configured.
Common Mistakes
- Forgetting the trailing slash or $request_uri – Without
$request_uri, the path is lost and users land on the homepage instead of the requested page. - Using
rewriteinstead ofreturn–return 301is more efficient and less error-prone than a regex rewrite for simple redirects. - Not listing all www domains – If you miss one, it won’t redirect. Double-check your DNS records.
- Redirecting the non-www domain to itself – Make sure your primary domain’s server block does not include a redirect back to www. Keep the redirect block separate.
- SSL certificate mismatch – If you use a single certificate for multiple domains, ensure it covers all of them (wildcard or SAN). Otherwise, browsers will show warnings.
- Editing the wrong config file – On some systems, the default config file might be overwritten on updates. Use a separate file in
sites-availableand symlink it tosites-enabled.
Tips
- Use a wildcard server_name – If you want to catch all subdomains (not just www), you can use
server_name *.example.comin the redirect block. But be careful: this will also catch other subdomains you might want to serve separately. - Consider HSTS – If you’re redirecting to HTTPS, you can add the
Strict-Transport-Securityheader on the primary domain to tell browsers to always use HTTPS. But do this only after you’ve confirmed HTTPS works. - Use a map for many domains – If you have dozens of domains, you can use a
mapdirective to map the host to the target, making the config cleaner. Example:map $host $redirect_target { default https://example.com$request_uri; www.example.com https://example.com$request_uri; www.example.net https://example.com$request_uri; } server { listen 80; server_name www.example.com www.example.net; return 301 $redirect_target; } - Test with curl – Use
curl -Ito see the full headers and confirm the redirect location. - Keep the redirect block simple – Don’t add extra directives like
rootorlocationblocks; they’re not needed and can cause confusion.
Troubleshooting
- Redirect not working – First, run
sudo nginx -tto check for syntax errors. If the config is fine, verify that the server block is actually loaded (e.g., it’s insites-enabledorconf.d). Check that the DNS for the www domains points to your server. - Getting a 404 instead of a redirect – This usually means the request is hitting the primary domain’s server block, not the redirect block. Ensure the
server_namein the redirect block matches exactly (includingwww). Also check that the primary domain block doesn’t have a catch-allserver_name _that intercepts all requests. - Redirect loop – If you get a loop, you’re likely redirecting the non-www domain to itself or back to www. Check that the primary domain’s server block does not contain a redirect to www. Also, if you have both HTTP and HTTPS redirects, make sure the HTTPS block for the primary domain doesn’t redirect to HTTP.
- SSL certificate errors – If you see certificate warnings, ensure the certificate covers the www domain being accessed. For multiple domains, you might need a SAN certificate or separate server blocks with their own certificates.
- Changes not taking effect – After editing, always reload Nginx. If you’re using a CDN or caching layer, clear that cache as well.
- Port 443 not listening – If you’ve added an HTTPS redirect block but it doesn’t work, check that Nginx is compiled with SSL support (
nginx -Vshould show--with-http_ssl_module). Also ensure your firewall allows port 443.
Frequently Asked Questions
Can I redirect all subdomains, not just www?
Yes, use server_name *.example.com in the redirect block. This will catch any subdomain and redirect it to the main domain. Be careful if you have other subdomains you want to serve separately.
Should I use 301 or 302 redirect?
Use 301 (Moved Permanently) for SEO purposes, as it tells search engines that the old URLs are permanently moved. 302 is temporary and not recommended for domain consolidation.
Do I need separate SSL certificates for each www domain?
Not necessarily. You can use a wildcard certificate (*.example.com) or a SAN certificate that includes multiple domains. If you have separate certificates, you’ll need separate server blocks or Nginx 1.15.10+ with multiple certificate directives.
What if I want to redirect to a different path, not just the same path?
You can modify the return directive to use a fixed path, e.g., return 301 https://example.com/new-location;. But this will send all requests to that path, losing the original URI. If you need conditional redirects, use a rewrite or a map.
Will this affect my email (MX) records?
No, web redirects only affect HTTP/HTTPS traffic. Email is handled by MX records, which are separate. However, if you use the same domain for email, ensure your mail server accepts mail for both www and non-www domains.
How long does it take for the redirect to be live?
Once you reload Nginx, the redirect is immediate. However, DNS changes (if you just added the www domains) can take up to 48 hours to propagate globally, though usually much faster.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.