Nowadays, If you are not using an SSL certificate on your website, visitors might not trust your website. Google too, prefers sites with SSL over the sites that do not have SSL certificates. In this tutorial, we will see how to redirect our website from http to https automatically for all the users. If you want to install SSL on Nginx or Install SSL on Apache, you can follow our guides.
It’s time to redirect our visitors from non-secure HTTP URLs to secure HTTPS URLs. There are many methods to perform this task. We will go with the best.
To redirect website visitors from HTTP to HTTPS forcefully, we just have to add few lines in the .htaccess
file on our server. You can create a .htaccess
file in the DocumentRoot of the domain or hostname you wish to redirect from HTTP to HTTPS. After creating the .htaccess
file, add the following lines in it. If you already have .htaccess
file available on your server, Just append these lines in the file.
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The above-given code will transfer any non-https connections to https using the same request and GET variables. And in the case of Cloudflare, there might be a small change in the code, only if your site is running through Cloudflare, your https requests may actually hit your server on HTTP port. Use the following code if you are using Cloudflare.
RewriteCond %{HTTPS} off RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
To Accept Globally in Apache
To apply this rule to all the domains under the server, follow the steps given below.
1) For accepting globally in the Apache, first of all, we need to create a global config file. The location of the file should be as given below.
/usr/local/directadmin/data/templates/custom/cust_httpd.CUSTOM.pre
Then add the following code in that file.
|?SSL_REDIRECT_HOST=www.`DOMAIN`| |*if SUB| |?SSL_REDIRECT_HOST=`SUB`.`DOMAIN`| |*endif| |*if SSL_TEMPLATE="1"| |?SSL_REDIRECT_HOST=| |*endif|
2) After that, we want to use the variable, so create a file at the following location with the same name.
/usr/local/directadmin/data/templates/custom/cust_httpd.CUSTOM.post
Now, Add the following code into the file in case of Apache.
|*if SSL_REDIRECT_HOST!=""| Redirect / https://|SSL_REDIRECT_HOST|/ |*endif|
In the case of Nginx, the code will be a little different. Here is the code for Nginx.
|*if SSL_TEMPLATE="0"| return 301 https://$host$request_uri; |*endif|
3) And if you want to enable https for all domain except one, then within the same code you can specify it. To disable it for only one domain name, go to:
Admin Level -> Custom Httpd Config -> domain.com
and in the CUSTOM token text area, add this text.
|?SSL_REDIRECT_HOST=|
This will make the variable blank and the specific domain name will be excluded from the rule.
4) Then need to rewrite the configs to use it.
cd /usr/local/directadmin/custombuild
./build rewrite_confs
So this is how you redirect website from HTTP to HTTPS. It is always a good practice to redirect from HTTP to HTTPS. It will protect the data that visitors will enter on your site. If you need any further help, please do reach our support department.