使用一个查询字符串将URL重写为另一个具有不同查询字符串的URL

时间:2013-04-12 20:50:14

标签: 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]

哪个工作正常。

但是,在源和目标中使用查询字符串重定向会将我返回到上面的目标网址。例如,

# 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。

1 个答案:

答案 0 :(得分:1)

您可以尝试这样做:

# 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]