In short
The same site can respond on four origins: http://example.com, http://www.example.com, https://example.com and https://www.example.com. The task is to choose one, redirect the other three to it with a direct permanent redirect, and align every signal (canonical, sitemap, internal links, CMS settings) with that choice. Once this is done, every URL on the site exists at a single address only.
Why it matters
For search engines, http://example.com/contact/ and https://www.example.com/contact/ are two different URLs. If both respond 200, every page on the site exists twice, or even four times. Google lists protocol variants among the usual causes of duplicates: it then groups the versions and picks one as canonical, generally preferring HTTPS, unless signals contradict it (invalid certificate, insecure resources, HTTPS that redirects to HTTP).
Leaving the decision to Google causes three problems:
- Scattered signals: external links point to several origins.
- Unpredictable choice: the selected canonical may vary from one page to the next, with a mix of
wwwand non-wwwin the results. - Security and trust: an accessible HTTP version exposes visitors and triggers browsers' "Not secure" warnings.
The choice between www and non-www has no effect on rankings. What matters is consistency. www has a technical advantage: it is a subdomain, which accepts a CNAME DNS record pointing to a CDN or platform, something the bare domain does not allow without a specific mechanism (ALIAS, CNAME flattening). Vercel and Netlify in fact recommend www as the primary domain.
What it improves
- Indexing A single version of each page in the index.
- Link equity Links to all four variants benefit the same URL thanks to the redirects.
- Security All traffic goes over HTTPS. See redirect HTTP to HTTPS (opens in a new tab).
- Measurement A single origin in the analytics tool and in reports.
- Generative engines Assistants cite a stable address. An indirect effect that cannot be measured in isolation.
- Rankings No direct effect from the choice itself.
How to check
Query the four origins on the same page, without following redirects:
for u in http://example.com/contact/ http://www.example.com/contact/ https://example.com/contact/ https://www.example.com/contact/; do
printf "%-36s " "$u"; curl -s -o /dev/null -w "%{http_code} %{redirect_url}\n" "$u"
done
Expected result if the chosen origin is https://www.example.com:
http://example.com/contact/ 301 https://www.example.com/contact/
http://www.example.com/contact/ 301 https://www.example.com/contact/
https://example.com/contact/ 301 https://www.example.com/contact/
https://www.example.com/contact/ 200
Points to check:
- A single hop: each variant points directly to the final URL.
http://example.com→https://example.com→https://www.example.comis a chain to fix. To follow the whole chain:curl -sIL http://example.com/contact/ | grep -i -E "^(HTTP|location)". - Path and parameters preserved:
http://example.com/contact/?a=1must land onhttps://www.example.com/contact/?a=1, not on the homepage. - Valid certificate on all four: the redirected HTTPS variant must present a certificate that covers its name, otherwise the browser shows an error before the redirect.
- Aligned signals: canonical, sitemap, internal links, hreflang and structured data all use the chosen origin.
In Search Console, a Domain property covers all four variants; see verify the domain property via DNS (opens in a new tab). The URL Inspection tool shows the canonical Google selected.
How to implement
Before you start
- Choose the origin: HTTPS is mandatory; with or without
wwwdepending on what already exists. Keep the version that is already most present in the index and in external links: switching costs you a migration for nothing. - Check the certificate: it must cover
example.comandwww.example.com. Let's Encrypt, hosting control panels and CDNs do this when you add both names. - Use a single 301 redirect per variant, as close to the entry point as possible: the CDN if there is one, otherwise the web server. Do not set it up in the CDN, the server and the CMS all at once.
- Then align: absolute internal links, sitemap, canonical, hreflang,
urlin structured data, configuration files (APP_URL,siteurl).
Warning: only enable HSTS (HTTP Strict Transport Security) once the redirects are stable and every subdomain is on HTTPS. Browsers remember the instruction for the specified duration and then refuse any HTTP access. See configure HSTS if appropriate (opens in a new tab).
The examples below use https://www.example.com. For the reverse, swap the two names.
Apache
Versions Apache 2.4.
In the HTTP virtual host, a simple redirect is enough:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
Redirect permanent / https://www.example.com/
# SSLEngine, SSLCertificateFile and SSLCertificateKeyFile as for www
</VirtualHost>
Redirect preserves the path and the query string. Without access to the configuration, in the .htaccess file at the root, before the CMS's rules:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]
Behind a CDN or proxy that terminates HTTPS, %{HTTPS} is always off and the rule loops. Replace the first condition with RewriteCond %{HTTP:X-Forwarded-Proto} !https. Reload with sudo apachectl configtest && sudo systemctl reload apache2.
Nginx
Versions Nginx 1.18 and later; http2 on; directive from 1.25.1.
In /etc/nginx/sites-available/example.com:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# site configuration
}
On an Nginx version earlier than 1.25.1, remove http2 on; and write listen 443 ssl http2;. Reload with sudo nginx -t && sudo systemctl reload nginx. Certbot (sudo certbot --nginx -d example.com -d www.example.com) produces a certificate valid for both names.
LiteSpeed
Versions LiteSpeed Web Server 6 and OpenLiteSpeed 1.7 and later.
LiteSpeed reads the rewrite rules in .htaccess: the second block in the Apache section works as is. In the OpenLiteSpeed WebAdmin console, enable rewriting and the option to read .htaccess on the virtual host, then perform a graceful restart.
Caddy
Versions Caddy 2.
Caddy obtains certificates and redirects HTTP to HTTPS automatically. Just add the name to redirect, in the Caddyfile:
example.com {
redir https://www.example.com{uri} permanent
}
www.example.com {
root * /var/www/example
file_server
}
permanent returns a 301 code. Reload with sudo systemctl reload caddy.
IIS
Versions IIS 10 (Windows Server 2016 to 2025), URL Rewrite module 2.1.
Install the URL Rewrite module, then in the site's web.config:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Canonical origin" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The query string is preserved by default (appendQueryString).
Cloudflare
Versions Cloudflare dashboard, all plans, November 2026.
Do everything at the edge, and nothing at the origin, to avoid loops:
- Encryption mode: in
SSL/TLS > Overview, choose "Full (strict)". "Flexible" mode connects to the origin over HTTP and causes loops if the origin redirects to HTTPS. - HTTP to HTTPS: in
SSL/TLS > Edge Certificates, enable "Always Use HTTPS". - Bare domain to www: from the rules overview,
Create rule > Redirect Rule, wildcard pattern. Request URL:https://example.com/*; target URL:https://www.example.com/${1}; status code 301; preserve the query string. For the reverse, Cloudflare documents the rulehttps://www.*tohttps://${1}.
Both names must exist in the DNS tab in proxied mode (orange cloud): an A or AAAA record for the bare domain, even a dummy one, if only www points to the origin.
AWS
Versions AWS console (CloudFront, Application Load Balancer, Amplify Hosting), November 2026.
CloudFront: in the distribution's behavior, set "Viewer protocol policy" to "Redirect HTTP to HTTPS". Declare both names as alternate domain names, with an ACM certificate (region us-east-1) that covers them. For the bare domain to www, associate a CloudFront Function with the viewer request event:
function handler(event) {
var request = event.request;
var host = request.headers.host.value;
if (host === 'www.example.com') {
return request;
}
var params = [];
for (var key in request.querystring) {
var entry = request.querystring[key];
var values = entry.multiValue ? entry.multiValue : [entry];
values.forEach(function (item) {
params.push(item.value === '' ? key : key + '=' + item.value);
});
}
var query = params.length ? '?' + params.join('&') : '';
return {
statusCode: 301,
statusDescription: 'Moved Permanently',
headers: { location: { value: 'https://www.example.com' + request.uri + query } },
};
}
Application Load Balancer: an HTTP:80 listener with the default action "Redirect to URL", protocol HTTPS, port 443, code HTTP_301; on the HTTPS:443 listener, a rule on the host header example.com that redirects to the host www.example.com. Amplify Hosting: in domain management, Amplify offers to redirect the bare domain to www.
Google Cloud
Versions Google Cloud console, external Application Load Balancer, November 2026.
On an external Application Load Balancer, create an HTTP-to-HTTPS redirect URL map (HTTPS redirect option with a 301 code, MOVED_PERMANENTLY_DEFAULT) attached to a forwarding rule on port 80. The bare domain to www is set up in the main URL map with a host rule that redirects.
Azure
Versions Azure portal, Azure Front Door Standard and Premium, App Service, November 2026.
On Azure Front Door, tick the option in the route to redirect all traffic to HTTPS, and add a rule set with a URL redirect action (301) for the host to redirect. On App Service, enable "HTTPS Only" in the app's TLS/SSL settings.
Plesk
Versions Plesk Obsidian 18.0.
In Websites & Domains > your domain, Hosting & DNS tab, open the hosting settings:
- Preferred domain: choose
www.example.comorexample.com. Plesk redirects the other name with a 301. - Permanent SEO-safe 301 redirect from HTTP to HTTPS: tick the box. It is enabled by default on new domains in Plesk Obsidian.
Install the certificate for both names (SSL/TLS Certificates, Let's Encrypt with the www option). Users report that with these two settings, http://example.com goes through two redirects; if your test shows this, replace them with the .htaccess block from the Apache section.
cPanel
Versions cPanel & WHM 110 and later, Jupiter theme.
In Domains > Domains, enable "Force HTTPS Redirect" on the domain; the option is greyed out until a certificate covers the domain (Security > SSL/TLS Status to run AutoSSL). cPanel has no dedicated setting for choosing www: paste the .htaccess block from the Apache section at the top of public_html/.htaccess. If you use that block, leave "Force HTTPS Redirect" disabled, otherwise http://example.com goes through two redirects.
Shared hosting
At OVHcloud, o2switch, Infomaniak, Ionos or Hostinger, first enable the free SSL certificate for the domain and www in the customer area, then paste the .htaccess block from the Apache section at the top of the file at the site root. Some control panels offer a "force HTTPS" checkbox: use either it or .htaccess, not both.
Vercel
Versions Vercel dashboard, all plans, November 2026.
In Project Settings > Domains, add both names, then click Edit on the one to redirect and choose the target under "Redirect to". HTTPS and the redirect from HTTP are automatic.
Netlify
Versions Netlify, all plans, November 2026.
In the site's domain management, set the primary domain: Netlify redirects the other name (www or bare) to it and forces HTTPS once the certificate is issued.
WordPress
Versions WordPress 6.4 and later, WP-CLI 2.
- In
Settings > General, fill in "WordPress Address (URL)" and "Site Address (URL)" with the chosen origin, for examplehttps://www.example.com. WordPress then redirects requests arriving on the other hostname. If the fields are greyed out, they are set inwp-config.php:
define( 'WP_HOME', 'https://www.example.com' );
define( 'WP_SITEURL', 'https://www.example.com' );
- Replace the old URLs in content with WP-CLI, after a backup:
wp search-replace 'http://example.com' 'https://www.example.com' --skip-columns=guid --dry-run
wp search-replace 'http://example.com' 'https://www.example.com' --skip-columns=guid
- Set up the HTTP-to-HTTPS redirect at the server or CDN, and clear the caching plugin's caches.
Shopify
Versions Shopify admin, all plans, November 2026.
In Settings > Domains, choose the primary domain (usually www.example.com). The other connected domains redirect to it, and Shopify provides the SSL certificate and the switch to HTTPS. Nothing else to configure; just check that the old .myshopify.com domain does redirect.
Laravel
Versions Laravel 10 to 13.
Set up the redirect at the web server or CDN. In the application, align URL generation:
APP_URL=https://www.example.com
Behind a proxy or load balancer, declare it so that Laravel detects HTTPS; where this setting goes depends on the version. As a last resort, URL::forceScheme('https') in the boot() method of AppServiceProvider forces generated URLs to HTTPS.
Laravel 11 to 13
In bootstrap/app.php:
->withMiddleware(function (Middleware $middleware): void {
$middleware->trustProxies(at: '*');
})
Restrict at: to your proxy's addresses if they are known.
Laravel 10 and earlier
In app/Http/Middleware/TrustProxies.php, which ships with the application, set the $proxies property:
protected $proxies = '*';
Replace '*' with the list of your proxy's addresses if they are known.
Next.js
Versions Next.js 15 and 16 (App Router and Pages Router).
Outside Vercel, add a host-based redirect in next.config.js (308 code with permanent: true):
module.exports = {
async redirects() {
return [
{
source: '/:path*',
has: [{ type: 'host', value: 'example.com' }],
destination: 'https://www.example.com/:path*',
permanent: true,
},
];
},
};
The switch to HTTPS happens at the proxy or CDN in front of next start.
Common mistakes
- A chain of two redirects: HTTP to HTTPS on the same name, then to
www. Each variant must target the final URL directly. See fix redirect chains (opens in a new tab). - Redirecting to the homepage:
http://example.com/contact/landing onhttps://www.example.com/loses the page. - 302 instead of 301: some control panels and plugins create temporary redirects by default.
- Certificate that does not cover the redirected name: visitors see a security warning before any redirect.
- Cloudflare Flexible mode with a redirect at the origin: an infinite redirect loop.
- Sitemap or canonical left on the old origin: the signals contradict the redirect. See align canonicals, sitemap and internal links (opens in a new tab).
- Mixed content: images or scripts still loaded over
http://. See fix mixed content (opens in a new tab).
Frequently asked questions
Is a site with or without www better for SEO?
Neither: Google does not favor www or the bare domain; only consistency matters. Keep the version that is already most present in the index and in external links, and redirect the other one with a 301. www has a technical advantage, since it accepts a CNAME record pointing to a CDN or hosting platform, which is why Vercel and Netlify recommend it as the primary domain.
Should you set a preferred domain in Search Console?
No, that setting no longer exists: the old preferred domain option was not carried over to the current Search Console. Google relies on your signals, namely 301 redirects, canonical tags, the sitemap and internal links. Ideally, create a Domain property, verified through DNS, which covers HTTP, HTTPS, www and non-www, and check the Google-selected canonical in the URL Inspection tool.
How long should HTTP and www redirects be kept?
Keep the HTTP-to-HTTPS redirects and the redirects between www and the bare domain permanently. They cost almost nothing, and old links, bookmarks and printed campaigns will keep using the old addresses for years. Removing them would bring back duplicates or errors. Only the configuration moves if you change host or CDN: test all four variants after every migration.
Why does my HTTPS redirect create a loop?
An HTTPS redirect loop almost always comes from a proxy or CDN that connects to the server over HTTP: the server believes the request is insecure and redirects endlessly. This is typical of Cloudflare's Flexible mode or a load balancer. Switch Cloudflare to Full (strict), test the X-Forwarded-Proto header instead of the HTTPS variable in the server, and do not stack redirects at the CDN, the server and the CMS.
Resources
- Google Search Central: Prefer HTTPS for canonical URLsdevelopers.google.com
- Google Search Central: Canonicalizationdevelopers.google.com
- Cloudflare: Always Use HTTPSdevelopers.cloudflare.com
- Cloudflare: Redirect www to rootdevelopers.cloudflare.com
- Vercel: Redirecting domainsvercel.com
- cPanel: Domains interfacedocs.cpanel.net
- Next.js: redirects in next.config.jsnextjs.org
Updated on
Topic checklist (PDF)