Redirect 如何重定向Nginx中的所有查询字符串url
我想将所有包含查询字符串(?)的url重定向到Nginx中查询字符串之前的url 例如:Redirect 如何重定向Nginx中的所有查询字符串url,redirect,nginx,url-rewriting,Redirect,Nginx,Url Rewriting,我想将所有包含查询字符串(?)的url重定向到Nginx中查询字符串之前的url 例如: example.com?asd to example.com example.com/?qwe至example.com/ example.com/post one/?sadh至example.com/post one/ example.com/hi/there/buddy?jbdg至example.com/hi/there/buddy 谢谢 更新: 找到此解决方案: if ($request_uri ~
example.com?asd to example.com
example.com/?qwe至example.com/
example.com/post one/?sadh至example.com/post one/
example.com/hi/there/buddy?jbdg至example.com/hi/there/buddy
谢谢
更新:
找到此解决方案:
if ($request_uri ~ ^/([^?]*)\?) {
return 301 /$1;
}
您添加的解决方案效率低下,因为它涉及对每个请求计算regex语句,然后对第一次匹配的任何请求进行重写和第二次regex计算 做得好比这简单得多。在Nginx中,
$args
变量保存查询字符串,因此,如果您每次只想在配置中添加此行时删除查询:
设置$args'代码>
就这样。没有更多的查询字符串
example.com/hi/there?kjg to example.com/hi/there
example.com/hi/there/buddy/?asgasg to example.com/hi/there/buddy/
if ($request_uri ~ ^/([^?]*)\?) {
return 301 /$1;
}