【问题标题】:How to redirect all query string url in Nginx如何在 Nginx 中重定向所有查询字符串 url
【发布时间】:2018-09-09 15:16:55
【问题描述】:

我想将 所有包含查询字符串 (?) 的 url 重定向到 Nginx 中查询字符串之前的 url。

例子:

example.com?asd to example.com

example.com/?qweexample.com/

example.com/post-one?pod to example.com/post-one

example.com/post-one/?sadhexample.com/post-one/

example.com/hi/there?kjg to example.com/hi/there

example.com/hi/there/buddy?jbdgexample.com/hi/there/buddy

example.com/hi/there/buddy/?asgasg to example.com/hi/there/buddy/

谢谢。

更新: 找到了这个解决方案:

if ($request_uri ~ ^/([^?]*)\?) {
   return 301 /$1; 
}

【问题讨论】:

    标签: redirect nginx url-rewriting


    【解决方案1】:

    您添加的解决方案效率低下,因为它涉及为每个请求评估正则表达式语句,然后为第一次匹配的任何请求重写和第二次评估正则表达式。

    正确地做到这一点要简单得多。在 Nginx 中,$args 变量保存查询字符串,因此,如果您只是尝试在每次将这一行添加到配置时删除查询:

    set $args '';

    就是这样。没有更多的查询字符串。

    【讨论】:

      猜你喜欢
      • 2019-06-17
      • 1970-01-01
      • 2018-03-15
      • 2016-07-05
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多