Worth noting the posted solution only works with Apache httpd. It won’t work with nginx, IIS, lighttpd, etc. Apache is usually the default choice for beginners on hosted servers, though.
Here’s nginx:
server {
listen 80;
server_name _;
rewrite ^ https://$server_name$request_uri? permanent;
}
Here’s lighttpd:
$HTTP["scheme"] == "http" {
$HTTP["host"] =~ ".*" {
url.redirect = (".*" => "https://%0$0")
}
}
In IIS, in SSL settings you check the box to require SSL.
The more general version of your Apache rule, which can be in htaccess, the server config, or the vhost config, is:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
If you have a very old version that doesn’t have %{HTTPS}, like 1.3, you should immediate dump that old garbage and upgrade. This is better than %{SERVER_PORT} 80 because it will capture other ports you might use (like 8080 is a popular alternative HTTP port).