nginx 重写 /forum 和 /forums
nginx rewrite /forum and /forums
我需要将 /forum 和 /forums 重定向到一个名为 /new 的新文件夹。这是为了捕获 name/type-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
试试这个:
location ~ ^/forums(.*)$ {
return 301 /new;
}
location ~ ^/forum(.*)$ {
return 301 /new;
}
并且 "return" 指令优于 "rewrite"。
我需要将 /forum 和 /forums 重定向到一个名为 /new 的新文件夹。这是为了捕获 name/type-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
试试这个:
location ~ ^/forums(.*)$ {
return 301 /new;
}
location ~ ^/forum(.*)$ {
return 301 /new;
}
并且 "return" 指令优于 "rewrite"。