htaccess 重写条件重定向到根域

htaccess rewrite condition redirect to root domain

我只是想将几个页面重定向到根域,但不确定为什么会失败,什么也没发生。

RewriteEngine On
RewriteCond %{QUERY_STRING} /category/uncategorized/
RewriteCond %{QUERY_STRING} /2014/12/
RewriteCond %{QUERY_STRING} attachment_id=19
RewriteCond %{QUERY_STRING} attachment_id=18
RewriteCond %{QUERY_STRING} attachment_id=5
RewriteCond %{QUERY_STRING} attachment_id=20
RewriteCond %{QUERY_STRING} attachment_id=72
RewriteCond %{QUERY_STRING} attachment_id=6
RewriteCond %{QUERY_STRING} attachment_id=10
RewriteCond %{QUERY_STRING} attachment_id=15
RewriteCond %{QUERY_STRING} attachment_id=14
RewriteCond %{QUERY_STRING} attachment_id=16
RewriteCond %{QUERY_STRING} attachment_id=17
RewriteCond %{QUERY_STRING} attachment_id=99
RewriteCond %{QUERY_STRING} attachment_id=44
RewriteRule http://domain.com? [L,R=301]

WP 重写在同一个 .htaccess 中,但如果我删除它们,则不会发生任何变化。

像下面这样的东西应该可以工作。确保它在 DocumentRoot

中的 .htaccess
RewriteEngine On
RewriteRule category/uncategorized/? http://domain.com? [L,R=301]

RewriteRule 2014/12/? http://domain.com? [L,R=301]

RewriteCond %{QUERY_STRING} attachment_id=([56]|1[4-9]|20|44|72|99)
RewriteRule ^ http://domain.com? [L,R=301]

编辑: @arco444 的答案对你有用(而且确实是一个更好的答案),但你必须尝试并准确理解什么是如果你想使用它就会发生。

选项 1

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/category/uncategorized/ [OR]
RewriteCond %{REQUEST_URI} ^/2014/12/ [OR]
RewriteCond %{QUERY_STRING} attachment_id=19 [OR]
RewriteCond %{QUERY_STRING} attachment_id=18 [OR]
RewriteCond %{QUERY_STRING} attachment_id=5 [OR]
RewriteCond %{QUERY_STRING} attachment_id=20 [OR]
RewriteCond %{QUERY_STRING} attachment_id=72 [OR]
RewriteCond %{QUERY_STRING} attachment_id=6 [OR]
RewriteCond %{QUERY_STRING} attachment_id=10 [OR]
RewriteCond %{QUERY_STRING} attachment_id=15 [OR]
RewriteCond %{QUERY_STRING} attachment_id=14 [OR]
RewriteCond %{QUERY_STRING} attachment_id=16 [OR]
RewriteCond %{QUERY_STRING} attachment_id=17 [OR]
RewriteCond %{QUERY_STRING} attachment_id=99 [OR]
RewriteCond %{QUERY_STRING} attachment_id=44
RewriteRule ^ http://domain.com? [L,R=301]

这里最主要的是 [OR] 标志。在您的原始代码中,您基本上是在说必须满足 all 条件。请记住,最后一个条件不应包含 [OR].

其次,您使用 QUERY_STRING 来检查 category/...,但这与出现在 ? 之后的查询字符串无关。所以,我将其更改为 %{REQUEST_URI}.

此外,您的 RewriteRule 没有可匹配的 URI。我在域名前加了一个^表示要匹配所有(也就是满足条件)

选项 2

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/category/uncategorized/ [OR]
RewriteCond %{REQUEST_URI} ^/2014/12/ [OR]
RewriteCond %{QUERY_STRING} attachment_id=(5|6|10|14|15|16|17|18|19|20|44|72|99)
RewriteRule ^ http://domain.com? [L,R=301]

在这里,我简化了查询字符串检查。我们现在使用一个条件来检查不同的附件 ID。

你或许可以进一步简化这个,但我回答的目的是让你知道你哪里错了,哪里可以稍微简化一下。