One of the easy way to redirect HTTP traffic to HTTPS using DNS
Hey guys, In this article I will discuss about redirecting HTTP traffic to HTTPS, After doing all the stuff at server for HTTPS and finally when you try to access your web application at HTTPS, It works like charm but not everyone is going to access your site with HTTPS and they would normally do yoursite.com which is a normal HTTP request at default port 80. So lets do the HTTPS redirection from HTTP.
There are many ways to setup HTTPS redirection such as using .htaccess configuration file for apache but there is one more easy way to do is by using your domain DNS service to redirect HTTP request to HTTPS, so in this article I’m using cloudflare which is a very good free CDN as well as DNS provider.
Step 1
Setting up a cloudflare account is very easy, you need to create an account, point your domain to cloudflare nameservers and add an A record for your server IP address, here is an article if you need more help with setting up an account on cloudflare.
Step 2
Now there are few things you need to understand here, as I said its a CDN network as well so your website traffic will go through the cloudflare network and it will serve static files faster to the users and increasing website speed.
Cloudflare provides SSL connection for free when your traffic is set to go through cloudflare it doesn’t matter if you are having an SSL or not. When you have an SSL certificate for your server and required to use it you will have to purchase a plan which allows custom SSL certificate, but it is only required when you want your traffic to go through cloudlfare.
Here the best part you can do is turning of CDN and just using the DNS service, and you will be able to use your custom SSL certificate installed on your server. When you have your cloudflare account set up go to the DNS tab and it should look like as in the below image. Click on the cloud icon at last in A record to turn of CDN as shown in the image below.
Now lets create a rule to redirect all traffic from HTTP to HTTPS. Navigate to Page Rules tab and type in a rule youdomain.com/* . Here * matches anything which you can use to match for your choice of urls, if you would like to match all subdomain then the rule can be *.yourdomain.com/*
. After writing your rule select Always use https to On and click on Add Rule. Take a look at the below image for reference.
You will also have to setup this rule when you choose your traffic through cloudflare and you need SSL.
This was a very small tip I just wanted to share with you geeks. Enjoy !