Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 有条件的;或;重写规则中的操作_Apache_.htaccess_Mod Rewrite_Httpd.conf - Fatal编程技术网

Apache 有条件的;或;重写规则中的操作

Apache 有条件的;或;重写规则中的操作,apache,.htaccess,mod-rewrite,httpd.conf,Apache,.htaccess,Mod Rewrite,Httpd.conf,我想将旧网站的某些URL重定向到新网站。此重定向到新网站是有条件的,即如果查询参数包含值为“eu”、“jp”或“in”的键“site”,则仅重定向,否则不重定向 RewriteCond %{QUERY_STRING} (?:^|&)site=(eu) [NC,OR] RewriteCond %{QUERY_STRING} (?:^|&)site=(in) [NC,OR] RewriteCond %{QUERY_STRING} (?:^|&)site=(jp) [NC

我想将旧网站的某些URL重定向到新网站。此重定向到新网站是有条件的,即如果查询参数包含值为“eu”、“jp”或“in”的键“site”,则仅重定向,否则不重定向

 RewriteCond %{QUERY_STRING} (?:^|&)site=(eu) [NC,OR]
 RewriteCond %{QUERY_STRING} (?:^|&)site=(in) [NC,OR]
 RewriteCond %{QUERY_STRING} (?:^|&)site=(jp) [NC]
 RewriteRule ^/?fetchHomePage.action$ https://example.com/%1? [R=301,L,NC]
 RewriteRule ^/?fetchFirstPage.action$ https://example.com/firstpage/%1? [R=301,L,NC]
 RewriteRule ^/?fetchSecondPage.action$ https://example.com/fetchsecondpage/%1? [R=301,L,NC]
 RewriteRule ^/?fetchThirdPage.action$ https://example.com/fetchThirdPage/%1? [R=301,L,NC]

通过以上配置,它可以很好地用于路径“/fetchHomePage”,但对于URL(“/fetchFirstPage”、“/fetchSecondPage”和“/fetchThirdPage”)中的其他路径,无论查询参数中有什么站点参数,浏览器总是重定向到新网站

您可以使用以下方法之一:

在这里,您需要为所有规则添加条件

RewriteCond %{QUERY_STRING} (?:^|&)site=(eu|in|jp)(?:&|$) [NC]
RewriteRule ^/?fetchHomePage.action$ https://example.com/%1? [R=301,L,NC]

RewriteCond %{QUERY_STRING} (?:^|&)site=(eu|in|jp)(?:&|$) [NC]
RewriteRule ^/?fetchFirstPage.action$ https://example.com/firstpage/%1? [R=301,L,NC]

RewriteCond %{QUERY_STRING} (?:^|&)site=(eu|in|jp)(?:&|$) [NC]
RewriteRule ^/?fetchSecondPage.action$ https://example.com/fetchsecondpage/%1? [R=301,L,NC]

RewriteCond %{QUERY_STRING} (?:^|&)site=(eu|in|jp)(?:&|$) [NC]
RewriteRule ^/?fetchThirdPage.action$ https://example.com/fetchThirdPage/%1? [R=301,L,NC]

您也可以编写多个规则,但为此,您需要从除最后一个规则之外的所有规则中删除
L
标志

RewriteCond %{QUERY_STRING} (?:^|&)site=(eu|in|jp)(?:&|$) [NC]
RewriteRule ^/?fetchHomePage.action$ https://example.com/%1? [R=301,NC]
RewriteRule ^/?fetchFirstPage.action$ https://example.com/firstpage/%1? [R=301,NC]
RewriteRule ^/?fetchSecondPage.action$ https://example.com/fetchsecondpage/%1? [R=301,NC]
RewriteRule ^/?fetchThirdPage.action$ https://example.com/fetchThirdPage/%1? [R=301,L,NC]

L=最后一个

[L]
标志导致停止处理规则集。在大多数上下文中,这意味着如果规则匹配,则不会处理进一步的规则。这对应于Perl中的最后一个命令,或C中的break命令。使用此标志指示应立即应用当前规则,而不考虑进一步的规则


参考资料:

在你的第二个建议中,只有第一个重写规则会受到RewriteCond的影响吗?另外,
[eu | in | jp]
是“e”、“u”、“i”、“n”、“j”和“p”中的一个字符。对于字符串“en”或“in”或“jp”,它是:
(?:^ |&)site=(eu | in | jp)(?:&$)
@julp感谢您的更正,我已经更新了答案。@DusanBajic我认为它应该对以下所有
RewriteRule
L的意思是“如果规则匹配,将不处理进一步的规则”,但我不明白为什么“/fetchHomePage.action”的规则会与包含“/fetchFirstPage.action”的URL匹配。您需要在每次重写规则之前再次添加条件。是的,看起来这是唯一的解决方案。