How to redirect HTTP Requests To HTTPS On Apache 2
We will describe how to set up Apache 2, and to redirect unsecure requests (HTTP) to secure (https).
Pre-requisites
- You have a Hukot Linux instance running Apache 2.
- Your have domain name (e.g example.com) whose DNS A records for "@" and "www" are pointing to the IP of your Hukot machine above.
- Ideally you should also have SSL setup on your instance.
Setup redirect
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]