How to Configure 301 Redirect from Non-WWW to WWW in Nginx
Configure high-performance 301 permanent redirects from non-WWW to canonical WWW domains in Nginx. Fix duplicate content indexing in Google Search Console and SSL wildcard issues.
Displayed below main page header or above the tool container. • Zero CLS Container
Automate & Test This in Our Free Tool
Eliminate syntax errors and test live URLs client-side using our dedicated Redirect & Regex URL Mapper.
The Technical Problem & Root Cause
Splitting domain authority between non-www and www hostnames creates duplicate content issues in Google Search Console and breaks SSL cert wildcard matching.
Duplicate without user-selected canonical (Google Search Console Coverage Report)
Production-Grade Solution & Code Snippet
Copy and paste this verified configuration directly into your project:
server {
listen 80;
listen 443 ssl http2;
server_name example.com;
return 301 https://www.example.com$request_uri;
}Step-by-Step Implementation Walkthrough
11. Add Dedicated Redirect Server Block
Add dedicated redirect server block above the primary server context in your Nginx configuration.
22. Catch Both HTTP and HTTPS Traffic
Match both HTTP and HTTPS listeners to catch all legacy non-www inbound traffic.
33. Validate Configuration and Reload
Test configuration with nginx -t and reload via systemctl reload nginx without dropping active connections.
Separates the interactive tool output from the deep technical guide. • Zero CLS Container
- Using an if ($host = example.com) condition inside the main block instead of a separate server block (triggers Nginx if-is-evil CPU overhead).
- Failing to include $request_uri in the return 301 directive, stripping deep link paths and query parameters.
- Omitting SSL certificates from port 443 redirect blocks, causing browser SSL handshake errors before redirecting.
- Creating redirect loops by defining server_name example.com in both the redirect and primary web blocks.
Frequently Asked Questions
What is the SEO impact of using 301 vs 302 redirects for domain canonicalization?▼
A 301 redirect is permanent and instructs search engine crawlers like Googlebot to transfer 100% of accumulated link equity and PageRank to the canonical WWW hostname. A 302 redirect is temporary, meaning search engines retain the old non-WWW index and divide ranking signals across both hostnames.
How do HSTS preload considerations affect non-WWW to WWW redirects?▼
If your domain is submitted to the HSTS preload list, browsers automatically upgrade all HTTP requests to HTTPS before hitting your server. Your Nginx config must serve a valid HTTPS certificate on the non-WWW domain to complete the 301 redirect to https://www.example.com.
Why do Cloudflare Page Rules conflict with server-level Nginx redirects?▼
If Cloudflare has an active Always Use HTTPS or Automatic HTTPS Rewrites rule conflicting with Nginx reverse proxy headers, requests can loop at the CDN edge. Ensure Cloudflare SSL mode is set to 'Full (Strict)' and redirect rules are handled at either the edge or the origin, not both.
Related Tools & Next Workflow Steps
Complementary utilities to streamline your SEO audit, indexing, and content strategy.
Bulk Canonical Normalizer & Auditor
Audit raw URLs in bulk, strip tracking parameters (UTMs/gclid), enforce trailing slash rules, and detect duplicate content risks.
XML Sitemap Generator & Validator
Generate standard XML sitemaps from URL batches, customize crawl priorities, and validate existing sitemap XML client-side.
Robots.txt Generator & Validator
Generate, test, and validate standard-compliant robots.txt files with live syntax checking, multi-user-agent rules, and sitemap directives.
Canonical URL Builder
Build self-referential and cross-domain canonical link tags to unify Google ranking signals and prevent duplicate content penalties.
Displayed below main page header or above the tool container. • Zero CLS Container