HostOnNet Blog

Redirect website to https using VirtualHost in Cpanel Server

Looking for Linux Server Admin or WordPress Expert? We can help.

I wanted to redirect web site to https. Normally, it you just have to create an .htaccess file with following content.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

In this case, i wanted to do it with out .htaccess, mainly because we use git for development and deploy, so don’t want to mess up with .htaccess on development machines. I could create a different .htaccess file for development, but it will be better handle through Apache VirtualHost entry.

Cpanel do not recommend editing httpd.conf, instead site specific changes go to its own file or these changes can be lost when cpanel update/rebuild httpd.conf etc..

First check VirtualHost entry for your domain in httpd.conf, you will find something like

    # To customize this VirtualHost use an include file at the following location
    # Include "/usr/local/apache/conf/userdata/std/2_4/hoston/*.conf"

You have to un comment the Include line as follows

    # To customize this VirtualHost use an include file at the following location
    Include "/usr/local/apache/conf/userdata/std/2_4/hoston/*.conf"

By default, this folder is not present, so lets create it

mkdir -p /usr/local/apache/conf/userdata/std/2_4/hoston/

You have to put your VirtualHost modifications on this folder, you can use any file name, but extension of the file must be .conf

Lets create file ssl.conf in the folder

vi /usr/local/apache/conf/userdata/std/2_4/hoston/

Add following content to the file.

RewriteCond %{HTTPS} off
RewriteRule (.*){REQUEST_URI}

You should replace with your own domain name.

Lets make edits to httpd.conf permanent by running

/usr/local/cpanel/bin/apache_conf_distiller --update

Restart Apache Webserver

service httpd restart

Now sites will redirect to SSL page, verify it with curl

boby@fwhlin:~ $ curl -I
HTTP/1.1 302 Found
Date: Thu, 01 Oct 2015 02:56:55 GMT
Server: Apache
Connection: close
Content-Type: text/html; charset=iso-8859-1

boby@fwhlin:~ $ 

Posted in Cpanel Server

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.