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

Welcome to TechBeginner.in, Tech Beginner provides technical blog for Online Linux Tutorials, Ubuntu Tutorials, Redhat Tutorials and CentOS 7, Configuration of server like Web Server, Application server, Database server, Window server, networking, html, css, java script, php, wordpress, opencart, magento, joomla and etc.

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

Share This


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

1 comment:

  1. An internet searcher that over and again gets a 404 mistake will de-list the page from its file. Such a large number of 404 blunder pages on your site and its positioning with the web indexes will endure.
    too many redirects error message

    ReplyDelete