Knowledge Base

How to redirect domain from HTTP to HTTPS in DirectAdmin?

Webhosting1st is offering reliable and cheap web hosting, prices are starting at just $10/year, for more details please check our plans.

1. I assume that You already enabled SSL certificate for Your website, for example Let's Encrypt (how to do it You can check HERE).

2. In Your /domains/domain.com/public_html directory create a file named .htaccess, if there is already such a file, edit it and add below code on top of the file.

3. Edit it and paste code:

RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

4. Save it and upload to server

Thats all, Your webite should redirect now all requests from http://domain.com to https://domain.com.

 

Adding www to Your domain.

If You wish to additionally add www to Your domain (http://example.com to https://www.example.com), above code will look like this:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ![0-9]$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

 

Removing www from Your domain.

If You wish to additionally remove www from Your domain (http://www.example.com to https://example.com), above code will look like this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

Please rate this article to help us improve our Knowledge Base.

4 0