Nginx 301重定向与URL中的圆括号

时间:2012-10-30 17:21:06

标签: redirect nginx rewrite

当有问题的网址有圆括号时,我很难在nginx中使用基本的301重定向。

通常,我只会使用这种类型的基本位置规则(不带括号):

location /abc/def {
rewrite /abc/def http://new.domain.com/abc/def/ permanent;
}

在上面提到的情况下,URL有圆括号:

source url:domain1.com/abc/def(ghi) 目标网址:domain2.com/abc/defghi

location /abc/def(ghi) {
rewrite /abc/def(ghi) http://new.domain2.com/abc/defghi permanent;
}

不幸的是,它并不像第一个例子那么简单。我已经多次修改规则以包括转义,urlencoded用于打开和关闭圆括号,正则表达式允许在括号中捕获单个字符,似乎没有任何效果。

逃离:

location /abc/def\(ghi\)

当URL有括号时,如何在nginx中使用301重定向?

2 个答案:

答案 0 :(得分:0)

location /abc/def(ghi) {
    return 301 http://new.domain2.com/abc/defghi;
}

答案 1 :(得分:0)

虽然在这个具体案例中,VBart的回答是有效的,但这并不是一般修复。

基本问题是()是正则表达式中的特殊字符, 对此的一般解决方法是逃避那些特殊的角色 但是问题是,\转义只有在"

中包含正则表达式时才有效 像这样:

location /abc/def(ghi) {
  rewrite "/abc/def\(ghi\)" http://new.domain2.com/abc/defghi permanent;
}
相关问题