Traffic Redirects: HTTP to HTTPS / www to non-www

March 1, 2017 | Views: 6116

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

When we deploy a website online for users, by default it allows the user to access the domain with and without “www”. This means that the same content is served by an apache server on both “kalilinuxhack.com” and “www.kalilinuxhack.com“. It is bad for SEO (Search Engine Optimization) as search engines consider this as two different domains with duplicate content. So, choose which domain you prefer, plain or www, and redirect the other one to the preferred domain. This type of redirect is called a Permanent Redirect, or “301 redirect,” and can be set up by configuring your DNS resource records and web server software.

 

There are lot’s of ways available to redirect traffic from “www” to “non-www” but in this tutorial, I will redirect the traffic on the apache server using an .htaccess file.

 

Requirement

  • DNS Manager
  • Public IP
  • Domain Name

 

In order to redirect the user from a non-www to a www and vice verse, you must setup an “A” record for each name.

 

 To add A record, first launch the DNS manager and click on add or create new button and fill the details as shown in below image:

 

 

 

Add another A record for “www” prefix of our site and fill the details as shown in below image:

 

 

 

Now that the website is accessible and points to both domains, the next main step is to redirect the domain using .htaccess file.

 

Firstly, we should enable the use of .htaccess file. For the apache server to do this, fire up terminal and open the apache configuration file using the below command:

 

sudo vi /etc/apache2/sites-enabled/000-default.conf

 

Add the below line in the configuration file by click on “i” keyword (to insert line in vi editor):

 

<Directory /var/www/html>
AllowOverride All
</Directory>

 

Now click on “esc” button 

then press “shift+:” 

and save the changes and exite using below command

 

“wq”  

 

(wq means write the changes and quit)

 

Now reload and restart the Apache server using below command:

 

sudo service apache2 reload
sudo service apache2 restart

 

We are almost done. Now go to the website root directory using below command:

 

cd /var/www/html

 

Now open .htaccess file using below command:

 

sudo vi .htaccess 

 

Then paste the below code in .htaccess file to redirect non-www to www and HTTP to HTTPS:

 

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.
RewriteCond %{HTTPS}s on(s)|offs()
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]

 

Save “.htaccess” file and exit

 

Now use the curl command to see whether redirection is working properly or not(non-www to www and HTTP to HTTPS)

 

curl -I http://abcd.com

 

Output:HTTP/1.1 301 Moved Permanently

 

 

Redirect www to Non-www and HTTP to HTTPS:

 

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.
RewriteCond %{HTTPS}s on(s)|offs()
RewriteRule ^ http%1://%1%{REQUEST_URI} [NE,L,R]

 

Now use the curl command to verify that the www domain is redirected to Non-www and HTTP is redirected to HTTPS

 

curl -I http://www.abcd.com

 

Output:HTTP/1.1 301 Moved Permanently

 

 

Note: SSL must be install setup and configure on apache server in order to redirect the traffic from HTTP to HTTPS.

 

Share with Friends
FacebookTwitterLinkedInEmail
Use Cybytes and
Tip the Author!
Join
Share with Friends
FacebookTwitterLinkedInEmail
Ready to share your knowledge and expertise?
4 Comments
  1. thanks verry helpfuuul

  2. this is more than great… thanks!

  3. great !!!!!!!!!!

  4. Useful and thorough… great synopsis, thanks!

Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Cybrary On The Go

Get the Cybrary app for Android for online and offline viewing of our lessons.

Get it on Google Play
 

Support Cybrary

Donate Here to Get This Month's Donor Badge

 
Skip to toolbar

We recommend always using caution when following any link

Are you sure you want to continue?

Continue
Cancel