Redirect Website /Path to Use HTTPS in DirectAdmin

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.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

What virtualization software do you use?

We use Parallels Virtuozzo Containers (PVC).

Why is my VPS constantly crashing?

There are several causes to why you may not be able to access your VPS. Some users often mistake...

What kind of support do you offer?

We offer Fully Managed VPS packages. For a list of items that we support, please refer to the...

What kind of software can I install on my VPS?

As you have full root access, you may install any compatible software on your VPS. However, any...

What is Virtuozzo?

Virtuozzo is the VPS technology created by SWsoft which allow us to create isolated VPS servers...