Remove parameters within nginx rewrite

前端 未结 4 1926
春和景丽
春和景丽 2020-12-09 09:09

I\'m rewriting URLs in nginx after a relaunch. In the old site I had query parameters in the URL to filter stuff e.g.

http://www.example.com/mypage.php?type=         


        
4条回答
  •  天命终不由人
    2020-12-09 10:08

    If you want to remove a specified parameter from url,

    #  in location directive: 
    if ($request_uri ~ "([^\?]*)\?(.*)unwanted=([^&]*)&?(.*)") {
        set $original_path $1; 
        set $args1 $2; 
        set $unwanted $3; 
        set $args2 $4; 
        set $args ""; 
    
        rewrite ^ "${original_path}?${args1}${args2}" permanent;
    }
    

    then visit your_site.com/a=1&unwanted=2&c=3

    step1. server gives an 302 response, indicating the url is match.

    step2. client re-send a request with the new url ( with the parameter removed)

提交回复
热议问题