Redirecting HTTP to HTTPS without DNS or static IP
Solution 1:
This is a simple redirection, so let's first avoid using mod_rewrite and use mod_alias Redirect
, instead.
If the
Redirect
directive is used within a<Location>
or<LocationMatch>
section with the URL-path omitted, then the URL parameter will be interpreted using expression syntax.
With the expression syntax you can use variables, and %{HTTP_HOST}
contains whatever there is in the Host:
header i.e. the hostname on the address bar of the browser, whether it is a DNS name or an IP address. That is exactly what you were looking for.
Let's put this together. Your default (first or only) name-based virtual host could have:
NameVirtualHost *:80
<VirtualHost *:80>
<Location "/">
Redirect "https://%{HTTP_HOST}%{REQUEST_URI}"
</Location>
</VirtualHost>
This will make any HTTP requests on port 80 redirect to its HTTPS equivalent, unless there is a matching name-based virtual host configured differently.