This writeup shows you how to setup Apache 2 to redirect non-secure (http) requests to secure (https) ones.
Make sure Apache's mod_rewrite module is enabled by running sudo a2enmod rewrite
.
Put below snippet in a .htaccess file in your site's root folder.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
Setup the http virtual host (at port 80) to forward to secure virtual host setup instead.
<VirtualHost *:80>
ServerName example.com
ServerAlias example.com www.example.com
Redirect 301 / https://example.com/
</VirtualHost>
<VirtualHost _default_:443>
ServerName example.com
SSLEngine On
#other vhost settings go here (e.g. ssl, logs, site root)
</VirtualHost>
Written by Lami Adabonyan
You could earn up to $300 by adding new articles