Apache answer both HTTP and HTTPS on the same port

I'm trying to get apache to redirect from http to https, however I want them both on the same port (20100, but I doubt that will matter). Basically what's going on here except I'm not doing this for webmin. Currently I have it configured to serve HTTPS, and when I access with HTTP I get:

Your browser sent a request that this server could not understand.
Reason: You're speaking plain HTTP to an SSL-enabled server port.
Instead use the HTTPS scheme to access this URL, please.

There seem to be a lot of somewhat similar questions, however I am unable to find one that actually answers my question.


Solution 1:

This is not going to be possible with Apache. With Apache you cannot have HTTPS and HTTP running on the same port.

I am aware of a few port-multiplexers designed to make HTTPS/OpenVPN and SSH run on the same port, but these require additional software.

  • SSLH: http://www.rutschle.net/tech/sslh.shtml
  • Multiplex: http://www.pond-weed.com/multiplex/ (appears to be dead)

Solution 2:

This can be archived by redirecting to a custom "400 - Bad Request" page and modify the redirection with a rewrite rule. In the following example i request

http://test.mydomain.com:27000

and get redirected to

https://test.mydomain.com:27000

with one virtual host.

Code:

ErrorDocument 400 /

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

Or you simply redirect directly to the HTTPS version of the page.

Code:

 ErrorDocument 400 https://test.mydomain.com:27000

But you will loose the ability to redirect on a real "Bad Request" page.