nginx URL rewrite using negative regex?

前端 未结 2 1856
故里飘歌
故里飘歌 2020-12-30 07:54

I\'m trying to redirect requests to https in nginx, unless it is of the form HOST/ANY_STRING_OF_CHARS/END_OF_URI, e.g.:

http://host.org/about # no redirect<

相关标签:
2条回答
  • 2020-12-30 08:18
    set $test "0";
    if ($request_uri ~ "condition") {
       set $test "1";
    }
    if ($test ~ "0") {
       return 301 redirect url;
    }
    
    0 讨论(0)
  • 2020-12-30 08:23

    Sends a permanent redirect to the client:

    server {
      listen 80;
      rewrite ^(/users/\w+)$ https://$host$1 permanent;
      ...
    }
    

    for negative match you could use:

    if ($request_uri !~ "^/users/\w+$")
    {
      return 301 https://$host$request_uri;
    }
    
    0 讨论(0)
提交回复
热议问题