
By: HakTuts
March 1, 2017
Traffic Redirects: HTTP to HTTPS / www to non-www

By: HakTuts
March 1, 2017
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 reloadsudo 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 onRewriteCond %{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 onRewriteCond %{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.