apache 在.htaccess中向同一行添加多个url参数时遇到问题

dy2hfwbg  于 2023-01-21  发布在  Apache
关注(0)|答案(1)|浏览(426)

当前我的.htaccess如下所示

RewriteCond %{QUERY_STRING} ^&
RewriteRule ^$ - [R=404]

RewriteCond %{QUERY_STRING} ^age
RewriteRule ^$ - [R=404]

RewriteCond %{QUERY_STRING} ^gender
RewriteRule ^$ - [R=404]

RewriteCond %{QUERY_STRING} ^languages
RewriteRule ^$ - [R=404]

RewriteCond %{QUERY_STRING} ^sites
RewriteRule ^$ - [R=404]

RewriteCond %{QUERY_STRING} ^sortOrder
RewriteRule ^$ - [R=404]

RewriteCond %{QUERY_STRING} ^status
RewriteRule ^$ - [R=404]

RewriteCond %{QUERY_STRING} ^tags
RewriteRule ^$ - [R=404]

目前这工作得很好,如果我访问一个URL与参数之一,它会给予我一个404页面,我想知道是否有一个更好的方式来写这一点。

是否可以将所有这些合并到一行中?

我试过这样写

RewriteCond %{QUERY_STRING} ^&
RewriteCond %{QUERY_STRING} ^age
RewriteCond %{QUERY_STRING} ^gender
RewriteCond %{QUERY_STRING} ^languages
RewriteCond %{QUERY_STRING} ^sites
RewriteCond %{QUERY_STRING} ^sortOrder
RewriteCond %{QUERY_STRING} ^status
RewriteCond %{QUERY_STRING} ^tags
RewriteRule ^$ - [R=404]

但这并不奏效,因为它只适用于顶部查询,而不适用于其余查询

ivqmmu1c

ivqmmu1c1#

我试过这样写

RewriteCond %{QUERY_STRING} ^&
RewriteCond %{QUERY_STRING} ^age
RewriteCond %{QUERY_STRING} ^gender
RewriteCond %{QUERY_STRING} ^languages
RewriteCond %{QUERY_STRING} ^sites
RewriteCond %{QUERY_STRING} ^sortOrder
RewriteCond %{QUERY_STRING} ^status
RewriteCond %{QUERY_STRING} ^tags
RewriteRule ^$ - [R=404]

RewriteCond指令(* conditions *)是隐式AND的,因此上述操作永远不会成功(即,不会出现404),因为查询字符串不能同时匹配所有这些字符串。
除最后一个 * condition * 外,您需要在所有其他 * condition * 上使用OR标志。例如:

RewriteCond %{QUERY_STRING} ^& [OR]
RewriteCond %{QUERY_STRING} ^age [OR]
RewriteCond %{QUERY_STRING} ^gender [OR]
RewriteCond %{QUERY_STRING} ^languages [OR]
RewriteCond %{QUERY_STRING} ^sites [OR]
RewriteCond %{QUERY_STRING} ^sortOrder [OR]
RewriteCond %{QUERY_STRING} ^status [OR]
RewriteCond %{QUERY_STRING} ^tags
RewriteRule ^$ - [R=404]

但是,这可以通过使用regex * alternate * 进一步减少。例如,上面的内容与下面的内容相同,只使用一个 * condition *:

RewriteCond %{QUERY_STRING} ^(&|age|gender|languages|sites|sortOrder|status|tags)
RewriteRule ^$ - [R=404]

相关问题