Project

General

Profile

Redirect to HTTPS using non-standard ports

Added by Josh Knox about 8 years ago

Hi,

I'm trying to get my app to automagically redirect from HTTP to HTTPS. For now, I'm using some non-standard ports.

I'm using wt-3.3.5 and the wthttpd connector. I'm using a self-signed ssl cert and that is working if I connect using https.

When I start my app I use these args to run http and https on different ports:

  --http-address 0.0.0.0 \
    --http-port 8081 \
    --https-address 0.0.0.0 \
    --https-port 8082 \

In my createApplication function, I check if we were called using "http" and redirect to "https":

if(app->environment().urlScheme() != "https") {
        std::string httpsUrl = "​https://" + app->environment().hostName() + app->url();
        std::cerr << "\nREDIRECTING TO HTTPS: " << httpsUrl;
        app->redirect(httpsUrl);
    } 

Unfortunately, with the redirect, I get a 404 error. The browser address bar reads:

http://127.0.0.1:8081/%E2%80%8Bhttps://127.0.0.1:8081

The new httpsUrl is using the http port:

REDIRECTING TO HTTPS: https://127.0.0.1:8081

Is there a way to get the base host name and the command line argument for "---https-port" vale so I can format the https address properly?

Thanks!

Josh


Replies (6)

RE: Redirect to HTTPS using non-standard ports - Added by Josh Knox about 8 years ago

OK, I can parse out the command line args on my own (or just hardcode them for testing) and put together the correct https address for my app.

For example, my server is running https on: "https://127.0.0.1:8082". If I put that in my browser address, it works great, my app loads, etc.

However, when I call

app->redirect("https://127.0.0.1:8082")

the browser address shows:

http://127.0.0.1:8081/%E2%80%8Bhttps://127.0.0.1:8082

and I get a 404 error page.

Why is the address in the browser getting screwed up?

Thanks!

Josh

RE: Redirect to HTTPS using non-standard ports - Added by Josh Knox about 8 years ago

This is weird. If I redirect to any other address it works fine and the browser address is correct.

If I redirect to a localhost address, the browser address has the HTTP address prepended to the HTTPS address in the browser.

Is localhost special?

Thanks!

Josh

RE: Redirect to HTTPS on same host fails - Added by Josh Knox about 8 years ago

Still stuck on this. Seems that if I try to redirect to an address on the same host (as localhost or via the fully qualified domain name), the address in the browser gets munged up as:

http://foobar.com:8081/%E2%80%8Bhttps://foobar.com:8082

If I redirect to some other host, the address in the browser is fine.

RE: Redirect to HTTPS using non-standard ports - Added by Josh Knox about 8 years ago

I looked at the GET contents generated by the redirect. It is using the original http address as the domain and using the https address as a filename:

http://127.0.0.1:8081/​https://127.0.0.1:8082/

Is this a bug?

RE: Redirect to HTTPS using non-standard ports - Added by Wim Dumon about 8 years ago

E2 80 8B seems to be a unicode character for a zero width space. Probably through some copy-paste mishap, the string https://127.0.0.1:8082/ in your source code includes this unicode character. Erase the string and retype it (type it, don't copy-paste it, to be sure).

Wim.

RE: Redirect to HTTPS using non-standard ports - Added by Josh Knox about 8 years ago

That was it! Works perfectly now. That was driving me crazy.

If you ever come to New Hampshire, I owe you a beer! ;)

Have a Good Weekend!

Josh

    (1-6/6)