8

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.

1
  • 1
    For those who find this question through Google: Serving both HTTP and HTTPS on the same port is totally possible — see github.com/mscdex/httpolyglot . The highly-voted negative answers are just saying that it's just not necessarily solvable using only Apache and no other tools at all. Mar 16, 2020 at 21:42

2 Answers 2

16

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.

7
  • Damn. Oh well. Would it be possible to have apache simply redirect to the HTTPS version of the page from the HTTP version?
    – Finn
    Feb 13, 2012 at 11:35
  • Sure. do a quick search here there are many questions that cover that.
    – Zoredache
    Feb 13, 2012 at 15:37
  • Yeah so I've discovered, however they all assume that you're running http on port 80 and https on port 443. None (that i could find) work for to redirect http requests to https on the same port.
    – Finn
    Feb 13, 2012 at 19:53
  • 2
    @thefinn93 The same port still won't work in Apache, regardless of what you're trying to answer with - a full page or a redirect. You need software in front of the listener that can make decisions on request routing based on the protocol the client sends - HAProxy may do the trick. But why go through all this effort - why not just use a different port? Feb 13, 2012 at 23:24
  • Alright. I assumed since apache was able to serve up something on that page it should be able to serve up redirect. Oh well, not really that important. Thanks!
    – Finn
    Feb 14, 2012 at 3:12
1

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.

5
  • Didn't want to vote down on this answer, but this approach doesn't work with Apache 2.4.51
    – Mladen B.
    Dec 5, 2021 at 8:15
  • i have the second code line successfully running for years on a Apache/2.4.7 (Ubuntu)
    – rdX
    Dec 6, 2021 at 10:40
  • Maybe your older version is responding with an other error code? If its 500 you just need to change the line.
    – rdX
    Dec 6, 2021 at 10:48
  • 2.4.51 is the currently latest Apache version
    – Mladen B.
    Dec 7, 2021 at 3:06
  • sry i meant your newer version responds with a different error code. I tested the second code line right now on Apache/2.4.51 (Unix) and it's working perfecly.
    – rdX
    Dec 8, 2021 at 6:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .