Redirect HTTP to HTTPS in Apache2 on ubuntu 22.04 LTS

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 22.04 LTS
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

Conclusion

We have successfully Redirect HTTP to HTTPS in Apache2 on ubuntu 22.04 LTS, Kindly report in case you are facing difficulties with following details.

  • OS name
  • OS version
  • Package name
  • Logs – Error / Warning / failed
Redirect HTTP to HTTPS in Apache2 on ubuntu 22.04 LTS

One thought on “Redirect HTTP to HTTPS in Apache2 on ubuntu 22.04 LTS

Leave a Reply

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

Scroll to top