SupportDomain Redirects

Redirect non-www to www: .htaccess, Nginx, Cloudflare & DNS

Direct Answer

Pick one host and send the other to it with a 301 that keeps the path. On Apache, add a RewriteRule to .htaccess; on Nginx, add a server block that returns 301 to https://www.example.com$request_uri; on Cloudflare, create a Redirect Rule from https://example.com/* to https://www.example.com/${1}. With no server to edit, point only the bare domain to RedirHub and create a 301 Domain Redirect to the www address.

example.com and www.example.com are two different hostnames, so search engines and browsers can treat them as two copies of your site. Google's advice for duplicate URLs is to pick one as the canonical URL and use server-side redirects to send traffic from the others to it. A 301 also merges links that point to either version.

Either host works for SEO; what matters is choosing one and redirecting the other everywhere: every path, the query string, and both http:// and https://. Below are the four common ways to do it, then how to test the result.

Which method fits your setup
Your site runs onUseWho serves HTTPS for the redirected name
Apache or shared hosting with cPanelA RewriteRule in .htaccessYour server: its certificate must cover both names
Nginx (VPS or your own server)A server block with return 301Your server: its certificate must cover both names
A site whose DNS is proxied by CloudflareA Redirect Rule (Single Redirect)Cloudflare's edge certificate
A host where you can't edit server config, or many domainsA RedirHub Domain RedirectRedirHub, issued and renewed automatically

Redirect non-www to www in .htaccess (Apache)#

Add these lines at the top of the .htaccess file in your site's document root (on cPanel hosting, usually public_html). mod_rewrite must be enabled, which it is on most shared hosts. Replace example.com with your domain.

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

For the opposite direction, www to non-www, swap the hostnames:

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

R=301 matters: without a number, mod_rewrite sends a temporary 302. The query string is passed on automatically. If you can edit the virtual host instead of .htaccess, Apache's own documentation recommends a Redirect directive in a separate VirtualHost for the name you are retiring.

Redirect non-www to www in Nginx#

Give the bare domain its own server block that only returns a 301, and keep your site in the www block. The HTTPS listener needs a certificate that covers example.com too, or visitors see a certificate warning before the redirect happens.

nginxserver {
    listen 80;
    listen 443 ssl;
    server_name example.com;
    ssl_certificate     /etc/ssl/example.com/fullchain.pem;
    ssl_certificate_key /etc/ssl/example.com/privkey.pem;
    return 301 https://www.example.com$request_uri;
}

server {
    listen 443 ssl;
    server_name www.example.com;
    # your site
}

For www to non-www, put www.example.com in the redirecting block and return 301 https://example.com$request_uri. Reload Nginx (nginx -t, then systemctl reload nginx) after saving.

Redirect non-www to www in Cloudflare#

Cloudflare's Redirect Rules (Single Redirects) only act on hostnames whose traffic Cloudflare proxies, so the record for the name you redirect away from must be Proxied (orange cloud).

  1. In DNS, make sure the record for example.com (the @ record) is set to Proxied.
  2. Go to Rules > Overview and select Create rule > Redirect Rule.
  3. Enter a Rule name, for example "non-www to www".
  4. Under When incoming requests match, choose Wildcard pattern and enter the Request URL https://example.com/*.
  5. Set the Target URL to https://www.example.com/${1}, the Status code to 301, and turn on Preserve query string.
  6. Select Deploy.
  7. To catch plain http:// requests too, turn on Always Use HTTPS under SSL/TLS > Edge Certificates.

For www to non-www, proxy the www record and use the Request URL https://www.example.com/* with the Target URL https://example.com/${1}.

Test the redirect#

Request the bare domain without following redirects and check that the answer is a single 301 whose Location is the www address, with the path kept:

bashcurl -sI http://example.com/about | grep -iE '^(HTTP|location)'
curl -sI https://example.com/about | grep -iE '^(HTTP|location)'

Both lines should show 301 and https://www.example.com/about. You can also run the domain through the redirect speed test to see the status code, the final URL, whether HTTPS is valid, and the response time from three regions in one go.

Is your redirect working? Enter your domain. You'll see the status code (301 or 302), where it lands, whether HTTPS is valid, and how fast it answers from Washington, Frankfurt and Tokyo, ranked against every redirect tested in the last 7 days.

Test my redirect

Redirect non-www to www with RedirHub (no server to edit)#

If your website lives on a host where you can't change server config, or you need the same rule on many domains, RedirHub can answer for the bare domain instead. Your DNS host and email stay where they are; only the record for the name you redirect away from changes, and RedirHub serves the 301 with automatic HTTPS from 130 global edge locations.

RedirHub New domain redirect form: Redirect from mybrand.com to https://www.mybrand.com, 301 Permanent and Keep the path selected; the What happens panel shows mybrand.com/about going to www.mybrand.com/about with a 301
  1. In Links, click Create and choose Domain Redirect.
  2. Under Redirect from, enter example.com.
  3. Under Redirect to, enter https://www.example.com.
  4. Choose 301 · Permanent and Keep the path.
  5. Open More options, then Query forwarding, and keep Forward query parameters on so UTM tags and other parameters reach the www page.
  6. Check the What happens panel: example.com/about should go to www.example.com/about with a 301. Click Create redirect.
  7. In Hostnames, open Connect DNS for example.com and change the A record at @ to the address it shows, plus the TXT record for verification. Remove any other A or AAAA records at @.
  8. Don't change the www record. www must keep pointing to the server that hosts your website.

Avoid a loop

A redirect for the bare domain also covers www whenever www points to RedirHub. If you point both names to RedirHub, www.example.com is redirected to itself and visitors see "too many redirects". Only the name you are redirecting away from should point to RedirHub.

Diagram of the DNS records for each direction: for non-www to www, the @ A record points to RedirHub and www keeps pointing to your website; for www to non-www, the www CNAME points to RedirHub and the @ record keeps pointing to your website

Redirect www to non-www with RedirHub#

  1. Create a Domain Redirect from www.example.com to https://example.com, with 301 · Permanent and Keep the path.
  2. In Connect DNS, set www as a CNAME to the target it shows, and remove any other record for www.
  3. Leave the A record at @ pointing to your website.

Server config or RedirHub?#

Redirecting the non-canonical host
Server config (.htaccess, Nginx) or CloudflareRedirHub
CostIncluded with your server or Cloudflare accountFrom $49/month (Core); 14-day free trial on Pro
Where it runsYour server, or Cloudflare if the site is proxied130 global edge locations
DNS and emailStay as they are (Cloudflare needs the record proxied)Stay where they are; only the redirected name's record changes
HTTPS for the redirected nameYou keep a certificate valid for both namesIssued and renewed automatically
Path and query stringKept if the rule is written correctlyKeep the path and Forward query parameters
Many domainsOne rule per server or zoneAll in one Links page, with CSV import on every plan
VisibilityServer logsClicks and unique visitors per link, plus a 404 report

If you already run Apache, Nginx or Cloudflare for the site, the free rule above is enough. RedirHub is worth it when there is no server you can edit, when the redirected name needs its own certificate, or when you manage the same canonical-host rule across many domains.

Teams with a portfolio of brand or parked and forwarded domains can set the same rule on all of them at once with a bulk CSV import.

Result#

Requests after the non-www to www redirect is live
RequestResponseLands on
http://example.com301https://www.example.com/
https://example.com/about301https://www.example.com/about
https://example.com/shop?utm_source=mail301https://www.example.com/shop?utm_source=mail (with Query forwarding on)
https://www.example.com/about200Your website, unchanged

Once DNS is in place, HTTPS for the redirected name is issued automatically; see Automatic HTTPS. To confirm a single 301 hop, trace the URL in the Redirect Checker. If you see a loop, read Fix redirect chains and loops.

Frequently asked questions

Neither ranks better. What matters is choosing one, redirecting the other to it with a 301 everywhere, and using that host in your canonical tags, sitemap and internal links.

No. A permanent server-side redirect is what Google recommends for consolidating duplicate URLs. Keep it to one hop, so avoid http://example.com → https://example.com → https://www.example.com chains where you can.

Use RewriteEngine On, then RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC] and RewriteRule ^(.*)$ https://example.com/$1 [L,R=301,NE]. Put it at the top of the .htaccess file in your document root.

Usually both names point to the same redirect. With RedirHub, a bare-domain redirect also covers www when www points to RedirHub, so www is sent to itself; point only the name you redirect away from to RedirHub. On a server, check that the condition only matches the host you are retiring.

On your own server, the rule must cover both port 80 and 443; on Cloudflare, turn on Always Use HTTPS. RedirHub answers both http:// and https:// for the domain and redirects both to the destination.

Many registrars can forward a domain, but often without HTTPS on the source or without keeping the path, so check both. RedirHub keeps the path and query string and issues HTTPS automatically.

Related help

View All Articles