How to automatically redirect HTTP to HTTPS on Apache servers? How to automatically redirect HTTP to HTTPS on Apache servers? apache apache

How to automatically redirect HTTP to HTTPS on Apache servers?


I have actually followed this example and it worked for me :)

NameVirtualHost *:80<VirtualHost *:80>   ServerName mysite.example.com   Redirect permanent / https://mysite.example.com/</VirtualHost><VirtualHost _default_:443>   ServerName mysite.example.com  DocumentRoot /usr/local/apache2/htdocs  SSLEngine On # etc...</VirtualHost>

Then do:

/etc/init.d/httpd restart


Searched for apache redirect http to https and landed here. This is what i did on ubuntu:

1) Enable modules

sudo a2enmod rewritesudo a2enmod ssl

2) Edit your site config

Edit file

/etc/apache2/sites-available/000-default.conf

Content should be:

<VirtualHost *:80>    RewriteEngine On    RewriteCond %{HTTPS} off    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}</VirtualHost><VirtualHost *:443>    SSLEngine on    SSLCertificateFile    <path to your crt file>    SSLCertificateKeyFile   <path to your private key file>    # Rest of your site config    # ...</VirtualHost>

3) Restart apache2

sudo service apache2 restart