由于问号符号“?”,.htaccess 301 重定向不起作用

.htaccess 301 redirect doesn't work due to question mark symbol "?"

我正在对我的旧网站使用以下代码,以 301 将所有流量重定向到我的新网站。

.htaccess - Located in oldsite.com

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^newsite.com [NC] 
RewriteRule ^(.*)$ http://newsite.com/ [R=301,L]
</IfModule>
Redirect 301 / http://newsite.com/

以上 .htaccess 代码仅在 url 不包含问号掩码符号“?”时有效。

例如

例1可以

http://oldsite.com/whatever ->301 Redirect to http://newsite.com/

但是,如果我要输入:

示例 2 不正确

http://oldsite.com/?whatever ->301 Redirect to http://newsite.com/?whatever

它应该像那样重定向到索引站点

http://oldsite.com/?whatever ->301 Redirect to http://newsite.com/

示例 3 不正确

http://oldsite.com/whatever?something ->301 Redirect to http://newsite.com/?something

它应该像那样重定向到索引站点

http://oldsite.com/whatever?something ->301 Redirect to http://newsite.com/

重定向和重写规则都是多余的。像这样使用你的规则。要删除 query string 您需要在重写规则的末尾添加一个问号。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^newsite.com [NC] 
RewriteRule ^(.*)$ http://newsite.com/? [R=301,L]
</IfModule>