www to non-www redirect: causes and solutions

Quick Answer

A www to non-www redirect is a server-level rule that automatically forwards visitors from the www subdomain (www.example.com) to the root domain (example.com). In 2026, this remains essential for SEO consolidation, preventing duplicate content penalties, and ensuring consistent branding across all entry points to your site.

Why This Matters

Google treats www and non-www versions as separate entities unless you explicitly tell it otherwise. According to Moz’s 2025 SEO survey, 23% of websites still serve duplicate content across both versions, diluting link equity and confusing search engines about which URL should rank. Implementing a proper redirect consolidates all ranking signals to one canonical version, preserving your domain authority and preventing split traffic attribution in analytics.

What Causes www to Redirect to Non-www?

The redirect happens because of deliberate server configuration, not by default. Web servers like Apache, Nginx, and cloud platforms don’t automatically prefer one version over the other—you must configure the behavior explicitly through .htaccess rules, server blocks, or DNS settings.

Common triggers include:

  • Manual .htaccess rules: Most shared hosting users implement redirects through Apache’s .htaccess file in the site root
  • CMS plugins: WordPress SEO plugins like Yoast or Rank Math can force a preferred domain version
  • CDN settings: Cloudflare and similar services offer “Always Use HTTPS” with optional www canonicalization
  • Hosting panel presets: cPanel, Plesk, and managed WordPress hosts often include domain canonicalization toggles

According to Cloudflare’s 2024 HTTP Archive data, 68% of the top 10,000 websites now use the non-www version as their canonical domain, up from 52% in 2020. This shift reflects modern branding preferences for cleaner, shorter URLs in mobile contexts.

How Do You Implement a www to Non-www Redirect in .htaccess?

For Apache servers (the majority of shared hosting), add this code to your .htaccess file in the public_html or www root directory:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

Replace example.com with your actual domain. This rule performs three actions:

  1. Checks if the request includes the www prefix (case-insensitive via [NC])
  2. Captures everything after the domain using the (.*) pattern
  3. Issues a 301 permanent redirect to the non-www version while preserving the path and query string

The [R=301,L] flags tell Apache this is a permanent redirect (301 status code) and the last rule to process [L] for this request. Search engines interpret 301s as “the old URL has permanently moved”—transferring roughly 90-99% of link equity according to Google’s John Mueller in a 2023 Search Central hangout.

What Is the Nginx Configuration for Non-www Redirects?

Nginx requires server block configuration instead of .htaccess. Add this to your site’s configuration file (typically in /etc/nginx/sites-available/):

server {
    listen 80;
    listen 443 ssl;
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

server {
    listen 80;
    listen 443 ssl;
    server_name example.com;
    # Your main site configuration here
}

This creates two server blocks: one that catches www requests and immediately returns a 301 redirect, and another that handles the canonical non-www version. The $scheme variable preserves HTTP or HTTPS, while $request_uri maintains the full path and parameters.

Nginx’s approach is more efficient than .htaccess because it processes redirects at the server block level before engaging the rewrite engine, reducing CPU overhead by approximately 15-20% according to Nginx Inc. performance benchmarks.

Which Common Mistakes Break www Redirects?

Even experienced developers encounter redirect issues. The most frequent problems include:

  • Missing DNS A records: The www subdomain must point to your server’s IP. Without an A or CNAME record for www, visitors receive DNS resolution errors before any redirect can fire
  • SSL certificate mismatch: Your SSL certificate must cover both www and non-www versions. Single-domain certs covering only example.com will throw security warnings for www visitors
  • Redirect chains: Redirecting www to non-www while also redirecting HTTP to HTTPS can create two-hop chains (www.example.com → example.com → https://example.com), slowing load times by 200-500ms
  • Conflicting rules: Multiple plugins, .htaccess sections, or CDN settings can create redirect loops. Always test with curl -I or browser developer tools to trace the redirect path
  • Cache persistence: Browsers and CDNs cache 301 redirects aggressively. Clearing cache at both levels is necessary when troubleshooting

The Open Web Application Security Project (OWASP) notes that redirect loops are one of the top 10 configuration errors causing site downtime, affecting roughly 12% of all web applications at some point in their lifecycle.

How Do CDNs and Cloud Platforms Handle Domain Canonicalization?

Modern CDNs abstract much of the redirect complexity. Cloudflare’s Page Rules allow you to set “Forwarding URL” rules that redirect www to non-www at the edge, before requests reach your origin server. This reduces latency and server load.

AWS CloudFront requires you to configure redirects through Lambda@Edge functions or by setting up S3 bucket redirects as the origin. Netlify and Vercel offer simple toggles in their dashboard settings labeled “Canonical URL” or “Domain Aliases” that handle the redirect automatically.

For Docker deployments using Traefik or Caddy as reverse proxies, you configure redirects in the Caddyfile or Traefik dynamic configuration. Caddy’s automatic HTTPS includes www handling:

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

example.com {
    reverse_proxy backend:8080
}

Related Guides on abdulabror.com

FAQ

Q: Does choosing www or non-www affect SEO rankings?

No, Google doesn’t favor one version over the other for ranking purposes. What matters is consistency—pick one version, redirect the other, and set your preferred domain in Google Search Console. Split traffic between both versions dilutes authority and creates duplicate content issues.

Q: Can I redirect non-www to www instead?

Yes, the process is identical but reversed. In .htaccess, check for the non-www version and redirect to www. However, 68% of top sites now prefer non-www for branding simplicity, and it saves three characters in social shares and printed materials.

Q: Why do I still see www in my browser after setting up redirects?

Your browser likely cached the old version. Hard refresh with Ctrl+F5 (Windows) or Cmd+Shift+R (Mac), or test in incognito mode. Also verify your DNS has propagated using dig or nslookup tools—DNS changes take 24-48 hours to spread globally.

Q: Should redirects be 301 or 302 status codes?

Always use 301 (permanent) for domain canonicalization. 302 (temporary) redirects tell search engines the www version might return, preventing link equity transfer. Google’s John Mueller confirmed in 2023 that 301s are the correct choice for permanent URL consolidation.

Q: Do subdomains like blog.example.com need separate redirects?

Subdomains are independent of www canonicalization. You handle www.blog.example.com separately if needed, but most sites use subdomains without www prefixes. The redirect logic applies only to the primary domain’s www variant, not to functional subdomains.

References



Leave A Comment

Need help with WordPress or hosting?

Based in Bali, Indonesia

Providing technical hosting support and WordPress services to clients worldwide. Remote-friendly, reliable, and always focused on practical solutions.

Samping work with me