nginx重写/论坛和/论坛

时间:2015-09-12 06:23:38

标签: redirect nginx rewrite

我需要将/ forum和/ forums重定向到名为/ new的新文件夹。这是为了捕获名称/类型-o的变体,它不能用作别名指令。

我需要这样:

/forum to /new
/forum/ to /new/
/forums to /new
/forums/ to /new/
/forum/blah to /new/blah
/forum/wee/blah.jpg to /new/wee/blah.jpg

我把头发拉了出来,我知道这很简单,但我已经尝试了十几个例子而且无法得到它。

使用:

location /forum {
    return 301 $scheme://$host/new;
}

仅适用于/ forum,它不适用于/ forum / blah例如(需要转至/ new / blah)。

02:22 AM [atlantis]/etc/nginx/sites root # curl -I xxx/forum
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Sat, 12 Sep 2015 06:22:06 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: http://xxx/new
Strict-Transport-Security: max-age=63072000
X-Content-Type-Options: nosniff

02:22 AM [atlantis]/etc/nginx/sites root # curl -I xxx/forum/blah
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Sat, 12 Sep 2015 06:22:12 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: http://xxx/new
Strict-Transport-Security: max-age=63072000
X-Content-Type-Options: nosniff

1 个答案:

答案 0 :(得分:1)

试试这个:

location ~ ^/forums(.*)$ {    
    return 301 /new$1;    
}

location ~ ^/forum(.*)$ {    
    return 301 /new$1;    
}

"返回"指令优于"重写"。