Apache 带有查询字符串的htaccess重定向

Apache 带有查询字符串的htaccess重定向,apache,.htaccess,mod-rewrite,query-string,url-redirection,Apache,.htaccess,Mod Rewrite,Query String,Url Redirection,我的访问规则是: RewriteCond %{QUERY_STRING} ^param1=val1 RewriteRule ^index\.php$ /page1 [L,R=301] 这将重定向到url:http://myhost/page1?param1=val1 如果我的url是:http://myhost/index.php?param1=val1¶m2=val2 应重定向到:http://myhost/page1?param2=val2 这意味着,它不应在重写条件中包含匹配的查

我的访问规则是:

RewriteCond %{QUERY_STRING} ^param1=val1
RewriteRule ^index\.php$ /page1 [L,R=301]
这将重定向到url:
http://myhost/page1?param1=val1

如果我的url是:
http://myhost/index.php?param1=val1¶m2=val2

应重定向到:
http://myhost/page1?param2=val2


这意味着,它不应在重写条件中包含匹配的查询字符串,只需使用组匹配:

RewriteCond %{QUERY_STRING} ^param1=val1(?:&(.+))?$
RewriteRule ^index\.php$ /page1?%1 [R=301,L,NC]
您可以使用:

RewriteCond %{QUERY_STRING} .*&?(param\d=val\d)
RewriteRule ^index\.php$ /page1?%1 [R=301,L,NC]
与:
http://myhost/index.php?param1=val1
->
http://myhost/page1?param1=val1


http://myhost/index.php?param1=val1¶m2=val2
->
http://myhost/page1?param2=val2


但仅当实名为param1、param2、param3…

^param1=val1(?:&(++)?$
而不带
&
时,它才起作用。如果它有参数。例如,如果我的url是:
http://myhost/index.php?param1=val1¶m2=val2
应重定向到:
http://myhost/page1?param2=val2
。这很好用。但是如果我有网址:
http://myhost/index.php?param1=val1
然后它将被重定向到
http://myhost/page1?%1
如果它没有parameters@Govind尝试将
B
标志与
R=301
标志一起使用。如果这不起作用,您必须将规则分成两个独立的规则。@hjpotter92:它不能使用
B
flag。因此,我通过两条规则实现了这一点。。谢谢你的帮助