Computer Tips - Apache: redirect users to your secure site (https)

Date: 2021jun19 Product: Apache Q. Apache: redirect users to your secure site (https) A. There are other variations of these directives out there but this is the best
<VirtualHost www.example.com:80> ... RewriteEngine On RewriteCond %{HTTPS} !=on RewriteCond %{SERVER_NAME} =www.example.com RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L] </VirtualHost>
- We check for the exact server name so we are sure its one we have a certificate for - You don't have to hardcode your domain name in the RewriteRule - This will redirect any URI on your non-secure site to the same URI on the secure site - Its not permanent (status code 301) which is safer in case you want to turn it off