【发布时间】:2013-04-05 12:39:04
【问题描述】:
我正在尝试完成一些重定向,如下所示:
#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_STRING} ^var=(.*)$ [NC] 和RewriteCond %{REQUEST_URI} ^/path/index.html [NC],但我仍然被重定向到http://newhost.com/Page?pagename=form1000。我交换了基本规则集和高级规则集的顺序,并全部重定向到http://newhost.com/Page?pagename=form1000。
关于如何让这些规则集发挥作用的任何想法? CentOS 6.x、Apache 2.2。
【问题讨论】:
-
规则的 URI 路径中不存在查询。您必须使用
RewriteCond %{QUERY_STRING}对其进行测试。 -
也许这个答案对stackoverflow.com/a/14899735/1741542有帮助
-
@faa:我确实用
RewriteCond %{QUERY_STRING} ^var=(.*) [NC]尝试过,结果相同。请注意,%1未在目标 URL 中使用。 -
@faa:你删除了你的答案吗?我能够使用它来使事情正常进行。如果你愿意,再发一次,我会接受的。谢谢你的帮助! (-:
标签: mod-rewrite apache2.2