+91 88 00 563434 [email protected] Blog open-book Knowledge Base

Find Solutions in Our Knowledge Base

Redirect Website /Path to Use HTTPS in DirectAdmin

You are here:
Sharing is Caring

Nowadays, If you are not utilizing an SSL certification on your website, visitors may not trust your website. Google also, prefers websites with SSL over the websites that do not have SSL certification. 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 finest.

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 want 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, Simply append these lines in the file.

RewriteEngine On
RewriteCond %HTTPS off
RewriteRule (.*) https://%HTTP_HOSTpercentREQUEST_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 would be a small change in the code, given that your website 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_HOSTpercentREQUEST_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"https://www.webhostingpeople.web/"*endif|

In the case of Nginx, the code will be a bit 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 just 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’s 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 assistance, please do reach our support department.


Sharing is Caring

Leave a Comment

Table of Contents