Mod rewrite 将具有一个查询字符串的URL重写为具有另一个查询字符串的URL

Mod rewrite 将具有一个查询字符串的URL重写为具有另一个查询字符串的URL,mod-rewrite,apache2.2,Mod Rewrite,Apache2.2,我正在尝试实现以下重定向: #Basic Rule Set RewriteRule ^/path$ http://newhost.com/Page?pagename=form1000 [R=301] RewriteRule ^/path/index.html$ http://newhost.com/Page?pagename=form1000 [R=301] 这很好用 但是,源和目标中带有查询字符串的重定向会将我返回到上述目标url。例如 # Advanced Rule Set Rewrit

我正在尝试实现以下重定向:

#Basic Rule Set
RewriteRule ^/path$ http://newhost.com/Page?pagename=form1000 [R=301]
RewriteRule ^/path/index.html$ http://newhost.com/Page?pagename=form1000 [R=301]
这很好用

但是,源和目标中带有查询字符串的重定向会将我返回到上述目标url。例如

# Advanced  Rule Set
RewriteRule ^/path/index.html?var=foo$ http://newhost.com/Page?pagename=form1000?id=hello  [R=301]
RewriteRule ^/path/index.html?var=bar$ http://newhost.com/Page?pagename=form1000?id=byebye [R=301]
RewriteRule ^/path/index.html?var=xyz$ http://newhost.com/Page?pagename=form1000?id=world  [R=301]
全部重定向到
http://newhost.com/Page?pagename=form1000
。 在上述三条规则之前,我尝试了
RewriteCond%{QUERY\u STRING}^var=(.*)$[NC]
RewriteCond%{REQUEST\u URI}^/path/index.html[NC]
,但我仍然被重定向到
http://newhost.com/Page?pagename=form1000
。我已经改变了基本和高级规则集的顺序,所有规则都重定向到
http://newhost.com/Page?pagename=form1000


关于如何让这些规则集发挥作用,有什么想法吗?CentOS 6.x、Apache 2.2。

您可以尝试以下方法:

# Advanced  Rule Set
RewriteCond %{QUERY_STRING} var=foo [NC]
RewriteRule ^path/index.html/? http://newhost.com/Page?pagename=form1000?id=hello  [R=301,L,NC]

RewriteCond %{QUERY_STRING} var=bar [NC]
RewriteRule ^path/index.html/? http://newhost.com/Page?pagename=form1000?id=byebye [R=301,L,NC]

RewriteCond %{QUERY_STRING} var=xyz [NC]
RewriteRule ^path/index.html/? http://newhost.com/Page?pagename=form1000?id=world  [R=301,L,NC]

规则的URI路径中不存在查询。你必须用
RewriteCond%{QUERY\u STRING}
来测试它。也许这个答案对@faa有帮助:我确实用
RewriteCond%{QUERY\u STRING}^var=(.*)[NC]
试过了,结果是一样的。注意,
%1
未在目标URL中使用。@faa:您是否删除了答案?我可以用它来让事情顺利进行。如果你愿意,再寄一次,我会接受的。谢谢你的帮助!(:如果有循环,请告诉我。@KM。规则是针对一个.htaccess文件的。如果它们在主配置文件中,则必须在
路径
中添加一个前导斜杠,就像您在问题中所做的那样。
^/path…
。如果有任何问题,请告诉我。谢谢@faa,这正是我要找的。您re welcome@KM。谢谢您在我删除它后告诉我。