条件“或” RewriteRule中的操作 [英] Conditional "OR" operation in RewriteRule

查看:94
本文介绍了条件“或” RewriteRule中的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to redirect certain urls of my old website to the new website. This redirection to new website is conditional i.e if query param contains key "site" with value as "eu", "jp" or "in" then only redirect otherwise do not.

 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),浏览器始终会重定向到新网站,而不管查询参数中的网站参数是什么。

With above configuration it works well for path "/fetchHomePage" but for other paths in the URLs ("/fetchFirstPage", "/fetchSecondPage" and "/fetchThirdPage") browser is always redirecting to the new website irrespective of what the site param is there in the query parameter.

推荐答案

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

You may follow one of these method :

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

Here you need to add the condition for all the rules.

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]

OR

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

You can also write multiple rules but for that you need to remove the L flag from all the rules except last rule.

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 = Last


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

The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed. This corresponds to the last command in Perl, or the break command in C. Use this flag to indicate that the current rule should be applied immediately without considering further rules.

参考: https:// httpd .apache.org / docs / 2.4 / rewrite / flags.html

这篇关于条件“或” RewriteRule中的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆