匹配相同参数的多个实例 mod_rewrite

Matching more than one instance of same parameter with mod_rewrite

我在重写规则方面遇到了一些问题。我有一个网站接受分页选项作为参数,作为任何位置的参数

index.php?page=2
index.php?other_parameter=other_value&page=2&another_paramenter=another_value

现在的问题是一些爬虫用来附加他们在页面上看到的所有参数,所以他们尝试

index.php?page=2&page=3&page=4
index.php?other_parameter=other_value&page=2&page=3&page=4&another_paramenter=another_value

我想阻止这些第二次尝试,所以基本上阻止任何在查询参数中包含多个 page=NN 实例的调用,可能会将它们重定向到主页。

我正在使用 https://technicalseo.com/tools/htaccess/ 来测试一些正则表达式来匹配这个,但我似乎无法找到一种方法来阻止任何具有多个 page= 参数的 url。

有什么提示吗?

您可以使用此规则来阻止此类查询字符串:

RewriteCond %{QUERY_STRING} (?:^|&)(page=)\d+(?=&(?:.*&)?) [NC]
RewriteRule ^ - [F]

RegEx Demo

此条件中的正则表达式匹配 page=<number>,前提是查询字符串中的任何位置后跟 page= 的另一个实例。如果匹配成功,则请求被拒绝 (403)。