Question

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?

Was it helpful?

Solution

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top