How to redirect with and without www?

A website can be reached with and without the www at the start of the domain name. Choosing one or the other on a new domain name will have no impact on SEO, but it is important not to leave both…

How to redirect with and without www?

A website can be reached with and without the www at the start of the domain name. Choosing one or the other on a new domain name will have no impact on SEO, but it is important not to leave both active at the same time, whatever the CMS (WordPress normally redirects to the address set in its settings, but it is better to enforce this at server level). It is also important to optimise your internal linking after a www redirect, to strengthen your site's SEO and user experience.

Reminder: It is preferable to put all your redirects in your vhost rather than in the .htaccess file, as we explained in our dedicated article; click here to find out more about the best place for redirects.

Preamble: it is important to remember that "global" redirects (which redirect a large number of URLs at once) must be placed after all the other redirects.

It is important to note that we are going to handle the redirect from http to https at the same time. So here we will distinguish the different cases:

Redirect from non-www to www in https:

By placing these 4 redirect lines you will make a 301 for every case where the site is not in https or on www:

RewriteCond %{HTTPS} off [OR]

RewriteCond %{HTTP_HOST} !^www\. [NC]

RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^.*$ https://www.%1%{REQUEST_URI} [L,NE,R=301]

Redirect from www to non-www in https:

The goal here is to send every variant without https or with www to the URL without www:

	RewriteCond %{HTTPS} off [OR]
	RewriteCond %{HTTP_HOST} ^www\. [NC]
	RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
	RewriteRule ^.*$ https://%1%{REQUEST_URI} [L,NE,R=301]

Turn redirect challenges into SEO opportunities. Swan.tools guides you towards tailor-made solutions.

Access the tool

Why do we not redirect from https to http?

Redirecting a domain from https to http is not really possible, because your users as well as Google will be faced with the invalid certificate error message when they land on the initial page in https.

The redirect from https to http will indeed be in place, but it will be blocked by this invalid SSL certificate security warning. The solution to work around this problem would be to generate an SSL certificate anyway, but if you have an SSL certificate it becomes a real shame not to favour the https version of the site. Note that since Let's Encrypt certificates are free (a service launched on the initiative of Mozilla, the EFF and the University of Michigan, and run by the ISRG), it would be a real shame not to switch your site to https.

Why avoid sites with and without www or with and without https?

By leaving your site reachable on both variants, you will create duplicate content. This is a problem in SEO because Google will see two "copies" of the content of each of your pages on two different URLs. Here we can even speak of two different domains. It is a problem because Google will have to choose for itself which version to index, and popularity signals (links, shares) will be split between the two.

Read next

All posts