Redirect all http page to https with www


Hello, it is very simple to redirect all HTTP requests to HTTPS with www. I am doing it here from the apache end however, you can do it from your DNS panel also.

First, you have to enable the mod_rewrite module in apache. By typing below command you can see which modules are enabled to your apache server.

# apachectl -M (For Ubuntu)
# httpd -M (For CentOs)

If the rewrite module is not there, then follow the below step to enable this.

For Ubuntu:

# a2enmod rewrite
# systemctl restart apache2

For CentOs:

# cd /etc/httpd/conf.modules.d/00-base.conf

Uncomment the below line

LoadModule rewrite_module modules/

# systemctl restart httpd

Now all the pre-requisite have been done to do the redirection. go to your virtual host file and copy the below rules between the virtual

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .*{REQUEST_URI} [L,R=permanent]

Restart the Apache server

# systemctl restart apache2 (ubuntu)
# systemctl restart httpd (CentOs)

You are done!

Leave a Reply

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