问题描述
我正在尝试设置Nginx重定向,其中domain.com/story/urlname
将重定向到domain.com/clientstories/urlname
。本质上需要从一个子路径重定向到另一子路径。我尝试了以下操作,但始终将我重定向到domain.com/clientstories(.*
,这里我想念什么?
location /story/(.*) {
return 301 $scheme://$server_name/clientstories$1;
}
解决方法
您的location
块缺少正则表达式运算符和URI开头的锚点。 return
语句在clientstories
之后缺少分隔符,因为它不是捕获的一部分。
使用正则表达式location
的示例:
location ^/story(/.*)$ {
return 301 /clientstories$1$is_args$args;
}
使用前缀location
的示例:
location /story/ {
rewrite ^/story(/.*)$ /clientstories$1 permanent;
}
正则表达式位置是按顺序计算的,并且优先于前缀位置,因此配置中其他位置的存在可能很重要。有关详细信息,请参见this document。