删除 nginx 重写中的参数 [英] Remove parameters within nginx rewrite
问题描述
我在重新启动后在 nginx 中重写 URL.在旧站点中,我在 URL 中有查询参数来过滤内容,例如
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
新页面没有这些参数.我想删除它们并将 URL 重写到主页,以便我得到:
The new page doesn't have these kind of parameters. I want to remove them and rewrite the URLs to the main page, so that I get:
http://www.example.com/mypage/
我在 nginx 中的重写规则是:
My rewrite rule in nginx is:
location ^~ /mypage.php {
rewrite ^/mypage.php$ http://www.example.com/mypage permanent;
}
但是有了这个规则,参数仍然被附加.我认为 $
会阻止 nginx 处理进一步的值......有什么想法吗?所有其他问题都涉及如何添加参数 - 我只想删除我的 :)
But with this rule the parameter is still appended. I thought the $
would stop nginx from processing further values... any ideas? All other questions deal with how to add parameters - I just want to remove mine :)
推荐答案
有一个类似的问题,经过大量搜索后,答案出现在 重写文档.
Had a similar problem, after a lot of searching the answer presented itself in the rewrite docs.
如果您指定一个 ?在重写结束时,Nginx 将删除原来的 $args(参数)
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;
}
这篇关于删除 nginx 重写中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!