新的永久链接结构:nginx重写循环

我在我的网站上build立一个新的永久链接结构。 添加了一些nginx重写规则后,有一个redirect循环到/ vlogger / info / info / info / info / …

老永久链接:

/vlogger/user-name 

新的永久链接:

 /vlogger/info/user-name 

目前的nginx规则:

 location ~ /vlogger/(.*)$ { rewrite ^/vlogger/info/$1 permanent; } 

我如何检查第二个目录(/ vlogger /“info /”)是否设置并redirect成功而没有循环?

(*用户名仅包含:a-z0-9和 – (减号))

Nginx终止在第一个匹配的正则expression式的search。 因此,只要在它上面放一个更具体的匹配。 您需要再次使用正则expression式匹配(imo),因为仅当没有正则expression式匹配时才使用前缀匹配。 (我强烈build议阅读手册: http : //nginx.org/en/docs/http/ngx_http_core_module.html#location )

 location ~ /vlogger/info/(.*)$ { # whatever you want to do with them } location ~ /vlogger/(.*)$ { rewrite ^ /vlogger/info/$1 permanent; } 

请注意,在你的情况下,正则expression式可能是没有必要的(如果你有其他的正则expression式,你可能需要使用正则expression式,再次阅读手册条目)。 前缀string可能会获得更好的性能…

 location /vlogger/info/ { # whatever you want to do with them } location /vlogger/ { rewrite ^ /vlogger/info/$1 permanent; } 

我find了解决办法:

 rewrite ^/vlogger/([^/]*)$ /vlogger/info/$1 permanent; 

这个从/ vlogger / nameredirect到/ vlogger / info / name