How to redirect HTTP to HTTPS and www to non-www

In the old days, website communication was hardly secured, and all websites’ URLs started with www. Nowadays most websites are secured using a SSL certificate, and most also dropped the www from their URL.

All good and well, but how can you ensure old links do not result in a 404 error?

In this – short – article, I show you how you make sure all old links are redirected correctly when you secure your site or want to drop the www from your site’s URL.

Redirecting your links from http to https

Sites that have existed for more than a couple of years will mostly have run some time without SSL security. Their default URL started with http://. Now these sites have installed SSL security, so now their default URL starts with https://.

However, there are a lot of links out there that point to that site still using http://, and many people still use http:// when they type in the URL. These links all need to redirect to the new URL for the same page.

You can create redirects for each page separately, but it is a lot easier to have a rule in you .htaccess file that does it for you automatically.

Note: do not change ANYTHING in your .htaccess file without having a working copy of the file on your desktop. Any typo in the file WILL bring your site down.

The code below takes any http link and redirects it to its exact equivalent starting with https://.

# 301 REDIRECT HTTP TO HTTPS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

Warning: when you use this code on a WordPress site and you have a plugin that redirects http to https, either turn off that feature or don’t use this code. It will create a loop of redirects, and you site will be down!

Redirecting your links from https to http

Even though I cannot think of a situation where you would want to do the reverse, here is the code to redirects 
an https link and redirects it to its exact equivalent starting with http://.

# Redirect HTTPS to HTTP
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

Redirecting your links from www to non-www

When websites were a new thing, all URLs started with www, which stands for World Wide Web. Nowadays, most websites drop the www and just use the shorter URL.

To redirect all incoming links using www to their equivalents without www, you can use this code in your .htaccess file:

# 301 REDIRECT WWW TO NON-WWW
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>

Redirecting your links from non-www to www

Of course, there will be websites that choose to have www in their URL.

To redirect all incoming links using without www to their equivalents that include www, use this code:

# 301 REDIRECT NON-WWW TO WWW
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]
</IfModule>

Redirecting your links from http to https and from www to non-www

The code snippets above do only one thing – and do it well – but you can also combine the code into one combined redirect.

The code below redirects any http link to its exact equivalent starting with https AND in the process redirects any www link to its equivalent without www in the URL:

# 301 REDIRECT HTTP TO HTTPS AND WWW TO NON-WWW
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
</IfModule>

Redirecting your links from http to https and from non-www to www

And to redirect any http link to its equivalent starting with https AND redirect any non-www link to its equivalent with www in the URL, use this code:

# 301 REDIRECT HTTP TO HTTPS AND NON-WWW TO WWW
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
</IfModule>

The snippets above are very handy to ensure a site’s URLs all keep working, but there are many more handy snippets you can use in your .htaccess file to make your life easier. Check out the many excellent snippets in the resources. Just make sure you have a working copy of your .htaccess file ready in case things go sour.

  • 0 användare blev hjälpta av detta svar
Hjälpte svaret dig?

Relaterade artiklar

How to install cPanel?

cPanel is a #1 Hosting Control Panel in the web hosting industry and you can easily install...

How to Install Plesk or Onyx on Linux CentOS?

Plesk is a popular website control panel and it makes managing your website easy. Although it is...

How to Install DirectAdmin on Linux CentOS?

DirectAdmin(DA) is a Commercial Hosting Control Panel and is used for selling shared and reseller...

How to Install Webmin on CentOS 7 Linux?

Webmin is a free hosting control panel with an easy installation. It takes only 5 to 10 minutes...

How to Install SolusVM Master on CentOS 7 Linux?

SolusVM is a popular VPS hosting control panel and SolusVM master is used for controlling Slave...