Redirect www URLs To non-www and HTTP To HTTPS

In this guide from the orcacore website, we intend to teach you How To Redirect www URLs To non-www and HTTP To HTTPS.

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

How To Redirect www URLs To non-www and HTTP To HTTPS

This guide will help you to make sure all old links are redirected correctly when you secure your site or want to drop the www from your site’s URL.

The old sites will mostly have run for some time without SSL security. Their default URL started with http://. Now, these sites have installed SSL security, so their default URL starts with https://.

But 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.

At this point, we will show you how to redirect to the new URL for the same page.

There is a way that you can create redirects for each page separately. But it is a lot easier to have a rule in your .htaccess file that does it for you automatically.

Important 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.

At this point, you can use the following code that 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 your site will be down!

Note: However, it is not recommended, but if you plan to redirect your links from HTTPS to HTTP, you can use the following code:

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

Now let’s see how to redirect your links from www to non-www.

Redirect www URLs 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.

At this point, you can use the following code in your .htaccess file to redirect all incoming links using www to non-www:

# 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>

On the other hand, some websites want to have www in their URL. To do this, you can use the following code in your .htaccess file:

# 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>

How To Combine the Redirect Codes

At this point, we want to show you how to combine the code into one combined redirect.

You can use the following code that 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>

You can use the following code that redirects 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>

Note: The codes above are very handy to ensure a site’s URLs all keep working, just make sure you have a working copy of your .htaccess file ready in case things go sour.

Conclusion

At this point, you learn to Redirect www URLs To non-www and HTTP To HTTPS with the .htaccess file. Also, you learn to combine the codes into one redirect.

Hope you enjoy it.

Please subscribe to us on Facebook and Twitter.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!