Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

Pick one HTTPS hostname—https://example.com or https://www.example.com—set WordPress to use it, and permanently redirect the other hostname to it. Neither format has an inherent SEO advantage. For a reliable setup, enforce the redirect at your web server, hosting platform, or CDN, then make sure WordPress, canonical tags, sitemaps, and internal links all use the same hostname.

What “www” and “non-www” mean

https://example.com and https://www.example.com are different hostnames, even if they display the same website. DNS, TLS certificates, browsers, cookies, caches, and search engines can treat them separately. Choose one as your canonical hostname and redirect the other to it.

There is no universal SEO ranking advantage to either choice. Non-www gives you a shorter public URL and often suits a standalone site. www can help distinguish the main website from other subdomains and may fit an organization’s existing DNS or infrastructure setup. If your branding, links, and integrations already consistently use one version, keeping it can reduce unnecessary change.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Before you change anything

  • Back up your WordPress files and database.
  • Confirm that both hostnames resolve to a service capable of redirecting requests. A missing DNS record cannot redirect.
  • Make sure the TLS certificate covers both hostnames. A visitor to https://www.example.com must complete TLS negotiation before receiving a redirect to https://example.com.
  • Identify where redirects are managed: your host, Apache, Nginx, or a CDN such as Cloudflare.
  • Check whether staging, API, or other subdomains must remain separate. Prefer rules that match the exact hostname rather than catching every subdomain.

Set the preferred hostname in WordPress

In the dashboard, go to Settings → General. Set both WordPress Address (URL) and Site Address (URL) to the same preferred HTTPS address. For non-www, use:

WordPress Address (URL): https://example.com
Site Address (URL):      https://example.com

For www, use:

WordPress Address (URL): https://www.example.com
Site Address (URL):      https://www.example.com

Use the complete URL and follow the trailing-slash convention already used by your installation; WordPress settings are commonly entered without a trailing slash. Save the changes, log in again if redirected, and clear relevant WordPress, browser, host, and CDN caches.

These settings tell WordPress which address to use for generated links and related output. They do not guarantee that every request to the alternate hostname is redirected before it reaches WordPress. WordPress core also attempts canonical redirects, but that runs within WordPress and has exclusions. Treat a server-, host-, or CDN-level redirect as the primary enforcement, not the dashboard setting alone. See WordPress’s canonical redirect reference.

If you cannot access the dashboard

You can define the values in wp-config.php. Add the matching pair before the line that says /* That's all, stop editing! Happy publishing. */. For non-www:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
define( 'WP_HOME', 'https://example.com' );
define( 'WP_SITEURL', 'https://example.com' );

For www, use https://www.example.com in both definitions. These constants override the corresponding database settings. Do not leave contradictory values in configuration and the database.

If you use WP-CLI, update both options. For non-www:

wp option update home 'https://example.com'
wp option update siteurl 'https://example.com'

For www, substitute https://www.example.com in both commands.

Redirect with Apache using .htaccess

Use these rules only on Apache or compatible hosting. Add the appropriate rule before the standard WordPress rewrite block, and replace the example domain with your own.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Redirect www to non-www

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

Redirect non-www to www

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

These exact-host rules preserve the requested path. Apache normally preserves the query string when the substitution does not add a new one; verify it with a URL such as https://www.example.com/page/?ref=test. The final URL should retain ?ref=test.

If your host already enforces HTTPS or a preferred domain, check its rules before adding another. Multiple layers can create a redirect chain or loop. The ideal outcome is a single redirect from each non-canonical variant to the final HTTPS URL.

Redirect with Nginx

Nginx redirects are configured in server blocks, not in .htaccess. For a non-www destination, configure the unwanted hostname to redirect directly to the canonical hostname:

server {
    listen 80;
    listen [::]:80;
    server_name www.example.com;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name www.example.com;

    # Configure a TLS certificate that covers www.example.com.
    return 301 https://example.com$request_uri;
}

For a www destination, use the apex hostname in the redirect blocks:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
server {
    listen 80;
    listen [::]:80;
    server_name example.com;
    return 301 https://www.example.com$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name example.com;

    # Configure a TLS certificate that covers example.com.
    return 301 https://www.example.com$request_uri;
}

The comments are important: the HTTPS redirect block cannot send a redirect until TLS succeeds, so the certificate must cover the hostname being redirected. Have your host or administrator validate the Nginx configuration before reloading it. Google recommends permanent server-side redirects for permanent URL changes; see its redirect guidance.

Use Cloudflare Redirect Rules

If your DNS and traffic are already managed through Cloudflare, a Redirect Rule can run before the request reaches WordPress. Cloudflare documents examples for redirecting www to the root hostname and redirecting the root hostname to www. Follow the current dashboard instructions for your account and rule type; use a permanent status such as 301 and preserve the path and query string.

Do not configure overlapping hostname redirects in Cloudflare, your host, and WordPress without testing. Conflicting rules or incorrect scheme detection at a reverse proxy can produce loops, chains, or inconsistent results. If Cloudflare terminates HTTPS upstream, also review WordPress’s HTTPS and reverse-proxy guidance.

If you cannot edit server or CDN settings

Check your hosting control panel for a canonical-domain or redirect setting. Labels and availability vary by provider, so do not assume a particular menu path. Ask the host whether it can redirect the alternate hostname directly to the final HTTPS URL and whether TLS covers both names.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A WordPress redirect plugin can help manage individual old URLs or migration mappings, but it is a weaker primary layer for hostname enforcement: it runs only after a request reaches WordPress. A plugin cannot fix a hostname that fails DNS or TLS, and it may not run if WordPress is unavailable. Server, host, or CDN configuration is preferable when available.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Align WordPress and search signals

After the redirect works, check that the chosen hostname appears consistently in:

  • Canonical tags and XML sitemaps.
  • Internal links, navigation, and feed links.
  • Open Graph, social-card, and structured-data URLs.
  • Media URLs and any absolute URLs stored in content.
  • Analytics and consent-cookie settings, especially if subdomains share cookies.

Google uses redirects, canonical annotations, and sitemap inclusion as canonicalization signals; consistent signals make the preferred URL clearer. Redirects are stronger than sitemap inclusion as a consolidation signal. See Google’s duplicate-URL consolidation guidance and its canonicalization overview.

If the hostname change affects indexing, verify and monitor both variants in Search Console. A Domain property covers the domain and its subdomains; a URL-prefix property covers only its specified protocol and hostname. Submit the sitemap using the preferred URL, inspect representative old and new URLs, and allow time for Google to recrawl and process redirects. Do not expect an instant index switch. Keep redirects in place long term, and follow Google’s site-move guidance if this is part of a wider URL migration.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Test all four hostname and protocol variants

For a non-www destination, the intended result is:

Request Expected result
http://example.com/ One permanent redirect to https://example.com/
http://www.example.com/ One permanent redirect to https://example.com/
https://www.example.com/ One permanent redirect to https://example.com/
https://example.com/ 200

For a www destination, reverse the hostnames. Use curl to check response headers:

curl -I http://example.com/
curl -I http://www.example.com/
curl -I https://example.com/
curl -I https://www.example.com/

Then check a deep URL and query string:

curl -I 'https://www.example.com/blog/example-post/?utm_source=test'
curl -I -L 'https://www.example.com/blog/example-post/?utm_source=test'

The first command should show a permanent redirect and a Location header pointing to the final hostname while retaining the path and query. The second follows the chain so you can confirm the final response. A successful setup has the preferred URL return 200, each alternate go directly to it, and no redirect back to the original hostname.

Troubleshooting

  • Redirect loop: Check that the condition matches only the unwanted hostname, that WordPress’s two URL values agree, and that CDN, host, and server rules do not contradict one another. Behind a reverse proxy, incorrect HTTPS detection can make WordPress repeatedly redirect; consult the WordPress proxy guidance.
  • Too many hops: Combine HTTPS and hostname enforcement so, for example, http://www.example.com redirects straight to https://example.com, rather than passing through HTTP non-www first.
  • Certificate warning: Install or issue a certificate that includes the alternate hostname. The browser must validate it before an HTTPS redirect can be returned.
  • DNS error: Add or correct the alternate hostname’s DNS record and ensure it reaches the redirecting host or CDN.
  • Broken assets or login after changing WordPress URLs: Confirm both WordPress URL fields point to the intended HTTPS hostname, check for conflicting WP_HOME or WP_SITEURL constants, and clear caches.
  • Unexpected staging or service redirect: Replace broad host matching with an exact hostname condition and review any shared CDN rules.
  • Stale browser result: Permanent redirects may be cached. Test with curl or a private browser window and purge relevant CDN or host caches.
  • Old URLs remain in content: If the hostname changed, update stored URLs with a serialization-aware migration tool after a backup. Do not run a raw SQL replacement on WordPress data. For example, preview a WP-CLI replacement first: wp search-replace 'https://www.example.com' 'https://example.com' --all-tables-with-prefix --skip-columns=guid --dry-run. Adapt the old and new values and options to the actual migration; remove --dry-run only after reviewing the report.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.