如何将网站根URL重写到新目的地?

时间:2010-09-17 03:29:02

标签: url-rewriting

使用urlrewiter.net我正在尝试将对网站root的任何请求进行301重定向到新目的地。 e.g

www.example.com应重定向到www.example.com/en /

我如何通过urlrewriter.net实现这一点我已经尝试了以下规则但没有工作或进入无限循环。

    <redirect url="^www\.example\.com" to="/en/" />
    <redirect url="^www\.example\.com\/" to="/en/" />
    <redirect url="^www.example.com" to="/en/" />
    <redirect url="^www.example.com/" to="/en/" />
    <redirect url="/" to="/en/" />
    <redirect url="^/" to="/en/" />

任何人都知道怎么做?

1 个答案:

答案 0 :(得分:0)

如果您想匹配“www.example.com”但不匹配“www.example.com/en”(从而避免无限递归),则需要在正则表达式的末尾添加$符号。尝试使用这三个规则:

<redirect url="^www\.example\.com$" to="www\.example\.com/en/" />
<redirect url="^example\.com$" to="www\.example\.com/en/" />
<redirect url="^www\.example\.com/$" to="www\.example\.com/en/" />

在末尾添加$符号,要求表达式匹配字符串的结尾。因此,通过在开头添加^,在结尾添加$,可以强制表达式为整个字符串。我认为你获得无限递归的原因是你使用所有匹配的模式 “www.example.com/en/”并将其重定向到自身,再将其重定向到自身等等。