Regex 使用nginx重写格式错误的URL

Regex 使用nginx重写格式错误的URL,regex,nginx,get,rewrite,Regex,Nginx,Get,Rewrite,我有一个脚本,它创建了一个带有特定链接的循环,如下所示: http://example.com/index.php?print=1 http://example.com/index.php?print=1?print=1 http://example.com/index.php?print=1?print=1?print=1 http://example.com/index.php?print=1?print=1?print=1?print=1 我希望有一个重写(301重定向)规则,将包含?p

我有一个脚本,它创建了一个带有特定链接的循环,如下所示:

http://example.com/index.php?print=1
http://example.com/index.php?print=1?print=1
http://example.com/index.php?print=1?print=1?print=1
http://example.com/index.php?print=1?print=1?print=1?print=1
我希望有一个重写(301重定向)规则,将包含?print=1的任何URL多次重定向到同一个URL,但只包含一个?print=1

例如:

http://example.com/index.php?print=1?print=1?print=1?print=1?print=1?print=1
重定向到

http://example.com/index.php?print=1
http://example.com/somefolder/?print=1
http://example.com/randomfilename.php?print=1

重定向到

http://example.com/index.php?print=1
http://example.com/somefolder/?print=1
http://example.com/randomfilename.php?print=1

重定向到

http://example.com/index.php?print=1
http://example.com/somefolder/?print=1
http://example.com/randomfilename.php?print=1

非常感谢您提供的任何帮助,nginx和regex/rewrites对我来说还是新的…

我不确定这是否有效,但我们可以尝试

将这些行添加到服务器块或
位置/
块中

if ($args ~ "(print=1\?print=1)") {
    return 301 $scheme://example.com$request_uri?print=1;
}

在我们解决这个问题之前,我们需要知道如何在nginx中检测它,单词
print
是我们需要检查的唯一单词吗?或者它是一个示例词?有没有可能会附加另一个变量?例如
?print=1?print=1?print=1?new variable=2
?它不仅是单词print,而且整个字符串“?print=1”被多次使用,没有使用其他GET变量。完美,只需稍加修改即可。我更新了你的答案,所以这是解决我问题的完美方案。谢谢!好的,我检查了你编辑的内容,你有一个点,
$args
不包含第一个
,既然我们要写
print=1?print=1
,那么我们就不需要数量
{1,}
,但我不明白为什么要用
$uri
替换
$request\u uri
,而且对
http
也是这样,但我更喜欢使用
$scheme
,我会用这两个更改编辑我的答案,有时间尝试一下,告诉我它是否有效