Remove parameters within nginx rewrite

北城余情 提交于 2019-11-29 01:17:11

Had a similar problem, after a lot of searching the answer presented itself in the rewrite docs.

If you specify a ? at the end of a rewrite then Nginx will drop the original $args (arguments)

So for your example, this would do the trick:

location ^~ /mypage.php {
    rewrite ^/mypage.php$ http://www.example.com/mypage? permanent;
}

To drop a parameter from a URL, in this case coupon=xxx:

if ($query_string ~ "^(.*)coupon=(.*)$") {
    rewrite ^(.*)$ $uri? permanent;
}

Note that this will drop all parameters if the statement matches. $uri is the original request without parameters.

Daniel Pérez Rada

Try setting the $args variable to empty inside the location.

set $args '';

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)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!