删除nginx重写中的参数

lor*_*key 19 rewrite get nginx

我重新启动后在nginx中重写URL.在旧网站中,我在URL中有查询参数来过滤内容,例如

http://www.example.com/mypage.php?type=4
Run Code Online (Sandbox Code Playgroud)

新页面没有这些参数.我想删除它们并将URL重写到主页面,以便我得到:

http://www.example.com/mypage/
Run Code Online (Sandbox Code Playgroud)

我在nginx中的重写规则是:

location ^~ /mypage.php {
    rewrite ^/mypage.php$ http://www.example.com/mypage permanent;
}
Run Code Online (Sandbox Code Playgroud)

但是使用此规则,参数仍会附加.我认为这$会阻止nginx处理更多的价值......任何想法?所有其他问题都涉及如何添加参数 - 我只想删除我的:)

小智 41

有一个类似的问题,经过大量的搜索后,答案在重写文档中呈现出来.

如果你指定一个?在重写结束时,Nginx将删除原始的$ args(参数)

所以对于你的例子,这可以做到这一点:

location ^~ /mypage.php {
    rewrite ^/mypage.php$ http://www.example.com/mypage? permanent;
}
Run Code Online (Sandbox Code Playgroud)

  • 我想删除一个特定参数怎么样? (16认同)

Luk*_*son 8

要从URL中删除参数,在这种情况下coupon=xxx:

if ($query_string ~ "^(.*)coupon=(.*)$") {
    rewrite ^(.*)$ $uri? permanent;
}
Run Code Online (Sandbox Code Playgroud)

请注意,如果语句匹配,这将删除所有参数. $uri是没有参数的原始请求.


Siw*_*申思维 6

如果要从 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;
}
Run Code Online (Sandbox Code Playgroud)

然后访问 your_site.com/a=1&unwanted=2&c=3

第1步。服务器给出 302 响应,表明 url 匹配。

第2步。客户端使用新的 url 重新发送请求(删除参数)


Dan*_*ada 5

尝试将$args变量设置为位置内的空。

set $args '';
Run Code Online (Sandbox Code Playgroud)