Redirect HTTP requests to HTTPS ?

bewatermyfriendxd
Posts: 21
Joined: Wed Apr 03, 2019 1:29 pm

Redirect HTTP requests to HTTPS ?

Postby bewatermyfriendxd » Fri Jun 14, 2019 7:14 pm

Hello,

I was developing a project recently which makes use of a http webserver and a wifi soft ap.
Now the project needs to support HTTPS. I was playing with an https server example project so far and noticed
that (on the client side) I have to type https:// manually every time when I want to call the server handlers.

Is there a way to implement a mechanism which will redirect all incoming http on the ESP32 side to HTTPS ?

I'll appreciate any help.
Thanks for the support.

bewatermyfriendxd
Posts: 21
Joined: Wed Apr 03, 2019 1:29 pm

Re: Redirect HTTP requests to HTTPS ?

Postby bewatermyfriendxd » Sat Jun 15, 2019 8:38 am

Update to my post above:

I was able to create a simple mechanism / workaround, that gets the job done, but Im not sure if this is an optimal solution:

I created the https server, which is listening on port 443 and provides a handler for the uri "/index".
I also created a http server, which is listening on port 80 and provides a handler for the uri "/".

When Im doing a request from my browser via http e.g "192.168.4.1" the http handler is called, which does https redirection:

Code: Select all

static esp_err_t redirect_handler(httpd_req_t *req)
{
    httpd_resp_set_type(req, "text/html");
    httpd_resp_set_status(req, "301 Moved Permanently");
    httpd_resp_set_hdr(req, "Location", "https://192.168.4.1:443/index");
    httpd_resp_send(req, NULL, 0);
    return ESP_OK;
}
Afterwards the redirection takes place and the webpage is served via https. From there the user is able to use the navigation elements on the website to keep browsing with https.

Im not sure if this very efficient in terms of memory consumption of the additional http server, which only provides redirecting mechanism.
I will adjust the config settings to optimize this.

If someone knows a better way or some important adjustments I have to make, please let me know.
Thanks

Who is online

Users browsing this forum: No registered users and 120 guests