Nginx

Nginx Rewite - 匹配的結束網址不起作用

  • January 21, 2015

嘗試重定向如下網址:

http://example.com/vehicles/cars/author/xyz
http://example.com/plane/author/xyz

對這些:

http://example.com/profile/xyz

我嘗試了這個 Nginx Rewrite,但它沒有選擇匹配並重定向:

rewrite ^/author/(.*)$     http://example.com/profile/$1 last;

我在這裡做錯了什麼?

^/author/(*)$表示它以 開頭/author/。您要重寫的 URL 不以/author/. 你需要這樣的東西:

rewrite ^/.*/author/(.*)$ http://example.com/profile/$1 last;

但我認為這更好,可能更健壯:

rewrite /author/([^/]+)/?$ http://example.com/profile/$1 last;

引用自:https://serverfault.com/questions/661346