Redirect URL中带圆括号的Nginx 301重定向
当所讨论的URL有圆括号时,我很难在nginx中获得基本的301重定向 通常,我只会使用这种类型的基本位置规则(不带括号): 在上述情况下,URL有圆括号:Redirect URL中带圆括号的Nginx 301重定向,redirect,nginx,rewrite,Redirect,Nginx,Rewrite,当所讨论的URL有圆括号时,我很难在nginx中获得基本的301重定向 通常,我只会使用这种类型的基本位置规则(不带括号): 在上述情况下,URL有圆括号: location /abc/def { rewrite /abc/def http://new.domain.com/abc/def/ permanent; } 来源url:domain1.com/abc/def(ghi) 目标url:domain2.com/abc/defghi location /abc/def(ghi) { rewr
location /abc/def {
rewrite /abc/def http://new.domain.com/abc/def/ permanent;
}
来源url:domain1.com/abc/def(ghi)
目标url:domain2.com/abc/defghi
location /abc/def(ghi) {
rewrite /abc/def(ghi) http://new.domain2.com/abc/defghi permanent;
}
不幸的是,它没有第一个例子那么简单。此后,我多次修改规则,包括转义、urlencoded用于开括号和闭括号、regex允许在括号中捕获单个字符,但似乎没有任何效果
转义途径:
location /abc/def\(ghi\)
当URL带有括号时,如何获得301重定向以在nginx中工作 虽然在这种特殊情况下,VBart的答案是有效的,但它不是一个通用的解决方案
location /abc/def(ghi) {
return 301 http://new.domain2.com/abc/defghi;
}
基本问题是(
和)
是正则表达式中的特殊字符,
一般的解决办法是摆脱那些特殊的角色
但是问题是,只有将正则表达式括在“
像这样:
location /abc/def(ghi) {
rewrite "/abc/def\(ghi\)" http://new.domain2.com/abc/defghi permanent;
}
非常感谢您的快速响应和解决方案!只有在将正则表达式括在
“
中时才有效。不,您错了。只有在正则表达式包含{
或;
章程时,它才应该用引号括起来。参见文档:问题是作者将\
放在之前(
和)
放在前缀位置
(不是正则表达式)。