How to redirect HTTP request to HTTPS in Apache Web Server and how this process is automated in HC

Question: How to automatically redirect HTTP request to HTTPS in Apache Web Server?
 
Answer: In this knowledge base article, we have explained manual mechanism to redirect websites from HTTP to HTTPS. Note that if you are using Hosting Controller control panel for managing your Linux websites then these steps are not required as we have explained in the end of this kb article.

Manual Steps: Using HC Panel interface when you enable SSL certificate on a website it will create only HTTPS (:443) entries in the apache web server. Those who do not want to map HTTP (:80) virtual host entry in apache then you can simply redirect the all non-secure web requests toward secure HTTPS by following below instructions:
  1. Access the Linux Server and go to path "/etc/httpd/conf.d"
     
  2. Create a file with name  redirect.conf
     
  3. Open the file in editor vi redirect.conf and make an entry for a website that you want to redirect automatically from HTTP to HTTPS

    </VirtualHost>
    <VirtualHost 192.168.0.54:80>
       ServerName linuxsite1.com
       Redirect permanent / https://linuxsite1.com/
    </VirtualHost>
    <VirtualHost 192.168.0.54:80>
       ServerName mysite2.com
       Redirect permanent / https://mysite2.com/
    </VirtualHost>

    Save the file and exit from editor
     
  4. Now open httpd.conf and include the redirect.conf file in it
    IncludeOptional conf.d/redirect.conf

    save and exit
     
  5. Restart httpd service

Automated process via HC panel : As also explained above Http to Https redirection is already automated in HC panel and you don't have to do any manual tweaks for it.

Upon installing SSL certificate on a website via HC panel , HC panel automatically adds a file domain.com.conf under "/etc/httpd/hc" with the mechanism to redirect any http request to that website towards https URL.If this is not somehow working on your website then open ticket by sending an email to support@hostingcontroller.com