How to redirect HTTP request to HTTPS in Apache Web Server

Question: How to automatically redirect HTTP request to HTTPS in Apache Web Server?
 
Answer:
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

Add Feedback