重定向任何路径,但只排除/

时间:2014-05-12 16:09:14

标签: nginx

我会将我的域名更改为另一个域名,只想提供服务" /"在旧域中,但其他路径将被重定向到url上下文后的新域。

location  ^/(.*)$ {
    # only serve /, other paths will be redirected to the new domain
    rewrite ^/(.*)$ https://new.com/$1 permanent;
}

location / { 
    # only serve a html in old.com/ to explain the domain change
    index   index.html;
}

这是重定向每个请求,包括根上下文,我缺少什么?

1 个答案:

答案 0 :(得分:1)

您错过了index进行内部重定向的部分,并且您总是在第一个位置结束。

有更优雅和nginx-y的方式来实现你的目标:

location = / {
    try_files /index.html =404;
}

location / {
    return 301 https://new.com$request_uri;
}
相关问题