To redirect www to non-www, pick the bare domain (example.com) as your canonical address, then make every request to www.example.com answer with a single HTTP 301 that points to the same path on example.com. Do it once, at one layer (server, control panel or CDN), and check it with a header request instead of a browser. This guide gives the rules for the common setups and the checks that catch loops, chains and half-working rules. Our Redirect Rule Generator writes the Nginx and Apache versions for you.
TL;DR
- Search engines treat example.com and www.example.com as separate URLs, so serving both splits links and traffic between two copies of your site.
- Use a permanent 301 (or 308) redirect from the non-canonical hostname to the canonical one. Google uses it as a signal for which URL to index.
- Configure the redirect in one place only. A .htaccess rule fighting a control-panel redirect is the classic cause of loops.
- Test with
curl -Iagainst all four variants (http/https, www/non-www) and expect exactly one hop. - Keep internal links, sitemaps and canonical tags on the same version you redirect to.
Table of Contents
- Why the two hostnames need one winner
- What a correct redirect looks like
- 301 or 302: why permanent matters
- Step 1: Confirm DNS and the certificate
- Step 2: Add the rule where you manage the site
- Step 3: Test with headers, not a browser
- Common problems and how to fix them
- Canonical tags and internal links
- Trailing slashes: the same logic, a different part of the URL
- Our view
- Check the result and keep watching it
- FAQ
- Sources
Why the two hostnames need one winner
Technically, www is just a subdomain. Browsers and crawlers treat example.com and www.example.com as different addresses, and nothing forces them to show the same page. If both answer with content, search engines can index both. SE Ranking points out that this splits link and traffic value between the duplicates and recommends a 301 from one hostname to the other.
MDN makes a practical point too: you cannot predict which version people will type or which one other sites will link to. So the advice in MDN's guide to choosing between www and non-www is to make both work. Choose the canonical type, then redirect the other one to it rather than letting it break.
Which one should you choose? Either is valid for search. A few things tip the decision:
- Non-www is shorter, matches how people say and print the domain, and is the right default for a new project with no history.
- www gives you a hostname you can point with a CNAME record. Web Hosting Canada, for instance, prefers www for CNAME flexibility on its own sites. Whether you need that depends on your DNS and CDN setup.
- Whatever is already indexed. If your site has been live for years on one version, moving to the other is a migration. Stay put unless there is a reason.
Once you decide, the rest of this article is about doing it cleanly.
What a correct redirect looks like
The target behavior is simple. A request for http://www.example.com/pricing?plan=pro should return a 301 with a Location header of https://example.com/pricing?plan=pro. The hostname changes, and the scheme moves to HTTPS in the same hop. The path and query string stay exactly as they were.
Three details separate a good rule from a bad one:
- The path survives. A rule that sends every www URL to the homepage throws away every deep link.
- The query string survives. Campaign tags and filters should pass through.
- One hop. The www URL should not go to http non-www, then to https non-www. Every extra hop slows the visitor and gives crawlers more to follow.
301 or 302: why permanent matters
Google's redirect documentation says 301 and 308 tell it the page has permanently moved, and that its indexing pipeline treats such a redirect as a signal that the target should be canonical. Temporary redirects (302, 303 and 307) do not carry that signal. The same page ranks the methods by how reliably Google reads them: server-side HTTP redirects first, then meta refresh, then JavaScript redirects. For a hostname change, use the server-side kind.

The 301 and 308 pair differ in one way: a 308 requires the client to repeat the same request method, while 301 lets clients change a POST to a GET. For a www redirect on ordinary page traffic, 301 is the standard choice. Our reference pages explain 301 Moved Permanently, 308 Permanent Redirect, 302 Found and 307 Temporary Redirect in detail, and our write-up on 301 vs 302 redirects covers the SEO side.
One warning about permanence: browsers cache 301 responses aggressively. If you test a wrong rule in a browser, you may keep seeing the old behavior after you fix it. That is why the testing section below uses curl.
Step 1: Confirm DNS and the certificate
A redirect only runs if the request reaches your server first. Before writing any rule:
- Make sure the hostname you are redirecting from has a DNS record. If www.example.com does not resolve, there is nothing to redirect. In Cloudflare's setup, that means the www record must exist in the DNS tab and be proxied so the redirect rule can see the request.
- Make sure your TLS certificate covers both hostnames. A visitor who types
https://www.example.comgets a certificate warning before your redirect can fire if the certificate lists only the bare domain. Run the domain through our SSL Checker and confirm both names appear.
This is the step people skip, and it explains many cases where the redirect works over http but fails over https.
Step 2: Add the rule where you manage the site
Use one of the methods below, not several. Pick the layer you already control.
Apache (.htaccess)
Flashcloud's knowledge base gives this pattern for forcing non-www:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
Place it near the top of the file. On WordPress, put it above the # BEGIN WordPress block, because WordPress rewrites that block when permalinks are saved and can wipe rules inside it. Escape the dots in the hostname, and keep [L,R=301] so the redirect is permanent rather than the default 302.
If you would rather not hand-write it, the Redirect Rule Generator outputs Apache and Nginx rules. Our Apache 301 redirect guide has more copy-paste patterns.
Nginx
The cleanest approach, described in LinuxCapable's Nginx guide, is a dedicated server block for the www hostname:
server {
listen 80;
listen 443 ssl;
server_name www.example.com;
# ssl_certificate lines here
return 301 https://example.com$request_uri;
}
return 301 beats rewrite here because only the hostname changes, while $request_uri carries the path and query string across unchanged. Reload Nginx after the edit. For more variations, including HTTPS and combined rules, see our Nginx rewrite examples and Nginx redirect rules.
cPanel
If you manage a shared hosting account, cPanel's Redirects screen is the simplest route. Per Web Hosting Canada's procedure, set the type to Permanent, enter the full destination without www, choose "Redirect only with www", tick wildcard redirection and click Add. Flashcloud notes this tool avoids typical hand-editing mistakes such as an unescaped dot, and that .htaccess suits you better when you already keep other redirect rules in that file.
The rule of thumb: if you use the cPanel tool, do not also add a .htaccess rule for the same job.
Cloudflare
Harrison Broadbent's Cloudflare walkthrough creates a Redirect Rule with the wildcard pattern https://www.*, the target https://${1}, status 301 and "Preserve query string" ticked. Cloudflare also ships a www-to-root template under Rules > Templates. Because the rule runs at the edge, requests never reach your origin, which is convenient but also means an origin-side rule will never be tested.
Managed platforms
Many hosting platforms offer a domain setting that marks one hostname as primary and redirects the other automatically. Where that exists, use it and skip the manual rules.
Step 3: Test with headers, not a browser
Browsers cache redirects and can hide a broken rule, so verify with a header request. Flashcloud suggests running:
curl -I https://www.example.com/
You want to see HTTP/1.1 301 and a Location header pointing to your chosen version. Then check inner pages too, since a narrowly written rule may catch only the homepage.
For a complete check, test all four combinations:
| Request | Expected result |
|---|---|
| http://example.com/page | One 301 to https://example.com/page |
| http://www.example.com/page | One 301 to https://example.com/page |
| https://www.example.com/page | One 301 to https://example.com/page |
| https://example.com/page | 200, the page itself |
This mirrors the four-variant procedure from LinuxCapable, which also says the chain should never bounce between hostnames. Adding -L to curl follows the whole chain so you can count hops.
If you are changing DNS at the same time, LinuxCapable suggests hitting the origin directly first with curl --resolve, so a CDN or proxy cannot mask what the server itself returns. Our guide to quick redirect checks walks through the same idea, and our HTTP response header inspection shows the status line and Location header without a terminal; you will find it among the tools on the Pingfloat home page.
Common problems and how to fix them
Redirect loops. Flashcloud warns that loops appear when a .htaccess www-to-non-www rule fights a non-www-to-www setting in cPanel. The fix is to use one method only and delete the other. Loops can also come from a CDN that connects to your origin over plain HTTP while the origin forces HTTPS. Browsers stop following after a fixed number of hops, commonly 20, and then show an error.
Chains. If http://www goes to http://non-www and then to https://non-www, you have a chain. Point old addresses straight at the final URL. The same reference lists HTTP instead of HTTPS, a trailing slash, an old cached 301 and chained redirects as the usual causes of unwanted redirect behavior.
Rules only on the homepage. A pattern such as ^$ matches the root and nothing else. Test an inner URL every time.
Lost query strings. In Cloudflare, tick "Preserve query string". In Nginx, use $request_uri rather than $uri.
Stale browser cache. After fixing a wrong 301, use a private window or curl. Cached permanent redirects can outlive the mistake on your own machine.

Canonical tags and internal links
A redirect is the strongest signal, but it works best when the rest of the site agrees with it. MDN documents a second technique: serve the same content on both hostnames and add a <link rel="canonical"> tag in the head pointing to the preferred URL. Unlike a 301, that leaves both URLs reachable in the browser, so it suits cases where you cannot redirect.
For your main site, combine both. Google's guidance, as reported by The Search Engine Post, is that self-referencing canonicals make clear which URL should be indexed, while 301 redirects remain the stronger signal for www versus non-www and http versus https. After you deploy, run your pages through a canonical tag checker to confirm every page points to the version you redirect to.
Also update:
- internal links, so they point straight at the canonical hostname and never rely on the redirect;
- your XML sitemap, which should list canonical URLs only;
- hreflang tags, social profiles and ad destinations.
Trailing slashes: the same logic, a different part of the URL
The trailing slash question is the same problem one level down. Google's To slash or not to slash post says it treats example.com/foo and example.com/foo/ as separate URLs. It adds that a trailing slash is more conventional for directories but either convention can be your choice. Pick one, use it in internal links and sitemaps, and 301 the other.
One exception: for the root, the slash is irrelevant. Search Engine Land quotes John Mueller saying that a slash after the hostname does not matter, while a slash anywhere else changes the URL.
In Apache, mod_dir already redirects real directories to the slash version by default, so extra rules are only needed for URLs that are not real directories. The Apache mod_rewrite documentation shows how to add or strip slashes with R=301.
The point that matters for www redirects: combine the hostname, scheme and slash fixes so a messy URL reaches its final form in one hop, not three. Test the combined behavior with the same curl table above, adding a slash and no-slash variant of an inner page.
Our view
Most redirect problems we see described are not about syntax. They come from rules stacked in several places and never checked from outside.
Our approach is to treat the redirect like any other piece of infrastructure: generate the rule, apply it in one layer, then verify it with headers rather than trust a browser. That is why our tools sit side by side: a redirect rule generator for Nginx and Apache, HTTP response header inspection, an SSL checker and an uptime monitor. Each handles one step of the same job, and all are free to start using without installing anything.
Once the redirect is live, keep watching the domain. A certificate that expires on the canonical hostname, or a server that stops answering, undoes the whole exercise.
Andreas
Check the result and keep watching it
The redirect rule itself takes a few lines. The part that matters long term is making sure it keeps working. Pingfloat is a free suite of web tools for developers and IT professionals, and several of them cover this job: the Redirect Rule Generator for the rule, the SSL Checker for the certificate on both hostnames, and the Website Uptime Monitor to keep an eye on the canonical address afterward. Core tools need no installation or account, and the rest of the toolkit is on the Pingfloat home page.

If you want to script checks of your own, the Pingfloat Public API is there for that.
FAQ
Should I redirect www to non-www or the other way around?
Both are valid, and what matters is picking one and sticking with it. Non-www is shorter and works well for new sites. Keep your existing version if it is already indexed and ranking, since switching is a migration.
Is a 301 or a 302 better for a www redirect?
Use a 301 (or 308). Google treats these as permanent moves and uses them as a signal for the canonical URL, while 302 and 307 are temporary and do not give that signal.
Why does my redirect work in curl but not in my browser?
Browsers cache 301 responses, so an earlier wrong rule can keep showing. Try a private window, clear the cache, or rely on curl -I, which shows what the server returns right now.
How do I avoid a redirect loop?
Configure the redirect in one place only. Loops usually appear when a .htaccess rule and a control-panel or CDN rule point in opposite directions. Remove one, then test all four http/https and www/non-www variants and confirm a single hop.
Do I need to redirect the trailing slash too?
If both /page and /page/ return content, yes. Google treats them as separate URLs, so choose one form, redirect the other with a 301 and use the chosen form in internal links and sitemaps.
Sources
- Choosing between www and non-www URLs, MDN
- Redirects and Google Search, Google Search Central
- To slash or not to slash, Google Search Central Blog
- Redirecting and Remapping with mod_rewrite, Apache HTTP Server 2.4
- Redirecting www to non-www (or the reverse), Flashcloud KB
- Redirect www to non-www (or vice-versa), Web Hosting Canada
- Nginx Redirect WWW to Non-WWW, LinuxCapable
- Redirect www. URLs to non-www within Cloudflare, Harrison Broadbent
- Duplicate Content: How To Find and Fix It, SE Ranking
- Google Recommends Using Self Referencing Canonicals, The Search Engine Post
- Google Search Console: How to fix Duplicate without user-selected canonical, Search Engine Land
- HTTP Status 301 (Moved Permanently), RESTful API
