Skip to content

URL Rewrites to Force Secure Connection #2183

Discussion options

You must be logged in to vote

If you need to send a 301, then send a 301: https://mongoose.ws/documentation/#mg_http_reply
AFAICS you don't have to force traffic, just tell the client to go to the secure URL and not serve pages at port 80. That means two listeners, one at port 80 returning 301 and the other at port 443 with TLS enabled
Something like:

void redir_fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
  if (ev == MG_EV_HTTP_MSG)
    mg_http_reply(c, 301, "Location: https://craftyourURLhere/\r\n", "");
}

void serve_fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
  if (ev == MG_EV_ACCEPT) {
    struct mg_tls_opts opts = {
      .cert = "cert.pem",    // Certificate file
 …

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
4 replies
@schilton
Comment options

@scaprile
Comment options

Answer selected by scaprile
@schilton
Comment options

@scaprile
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants