Redirect HTTP to HTTPS in Apache2 on ubuntu 16.04/18.04/20.04

Apache HTTP worker is one of the most mainstream web workers on the planet. It is an open-source and cross-stage HTTP worker that controls an enormous level of the Internet’s sites. Apache gives numerous incredible highlights that can be reached out through extra modules.
On the off chance that you are a site proprietor or framework head, odds are that you’re managing Apache all the time. One of the most widely recognized assignments you’ll likely perform is diverting the HTTP traffic to the made sure about (HTTPS) form of your site.

Install rewrite module 
$ sudo a2enmod rewrite
Redirect HTTP to HTTPS on Apache2 Virtual Host using ubuntu 16.04/18.04/20.04
Edit virtualhost of port 80 and add the following lines like this.
Redirect / https://www.example.com
For a example
<VirtualHost *:80>
   ServerName www.yourdomain.com
   Redirect / https://www.yourdomain.com
</VirtualHost>
Configure your port 443 for HTTPS 
You need to configure ssl on your apache2 web server to redirect HTTP web traffic redirect to HTTPS like this.
<VirtualHost _default_:443>
   ServerName www.example.com
   DocumentRoot /var/www/html/
   SSLEngine On
   # SSL configure here
</VirtualHost>
Save and Exit from the Apache2 virtual configuration file.
Check Apache Configuration file
$ sudo apachectl configtest
Reload the Apache2 Services 
$ sudo systemctl reload apache2
Redirect HTTP to HTTPS using .htaccess
Go to Root Document directory and create .htaccess files and add the following redirection lines.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
Reload the Apache2 Services 
$ sudo systemctl reload apache2
Redirect HTTP to HTTPS in Apache2 on ubuntu 16.04/18.04/20.04

One thought on “Redirect HTTP to HTTPS in Apache2 on ubuntu 16.04/18.04/20.04

Leave a Reply

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

Scroll to top