문제

I will be changing my domain for another, only want to serve "/" in the old domain, but other paths will be redirected to the new domain following the url context.

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;
}

this is redirecting every request, including root context, what I am missing?

도움이 되었습니까?

해결책

You missed the part that index makes internal redirect and you always end up in first location.

There is more elegant and nginx-y way to achieve your goal:

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

location / {
    return 301 https://new.com$request_uri;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top