Redirect All HTTP URLs to HTTPS while Preserving Path on Apache with mod_rewrite


To ensure all requests to a service end up shoved onto HTTPs, whether they come from errant resource calls on a webpage or the casual user simply entering the hostname in their browser's addressbar without specifying the protocol or having an always-use-https browser plugin (you know, normal folk), you can use the following snippet in your httpd.conf, VirtualHost blocks or .htaccess files in DocumentRoots (or anywhere else, for reasons unknown) and it will make sure they end up getting to where they originally intendeds to go - but with TLS.

RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [R,L]

Now head on over to Let's Encrypt and get yourself a nice, free 90 day SSL certificate for your hostname or, if you prefer, a wildcard cert to plaster on everything that ends in your domain. Try to get certbot to automatically renew it automagically - so you're not driven mental every three months.


There are no comments for this item.