301 Redirect Specific Pages in Nginx BEFORE Redirecting Entire Domain/Catch All
The directives of the rewrite module are executed in order, so the entire process can be accomplished with rewrite
and return
directives alone, without wrapping them within location
blocks. For example:
server { ... rewrite ^/old-path/ https://www.newdomain.com/newpath/ permanent; rewrite ^/... https://www.newdomain.com/.../ permanent; return 301 https://www.newdomain.com$request_uri;}
If you wrap the rewrite
statements within location
blocks, then the return
statement must also be wrapped within a location
block. For example:
location / { return 301 https://www.newdomain.com$request_uri;}location /old-path/ { rewrite ^ https://www.newdomain.com/newpath/ permanent;}