My goal is to redirect all API requests (both GET or POST) from old-domain.com
to new-domain.com
but only from specific GET company_id
value (ABC
). We will still treat other requests other than that to old-domain.com
.
For example :
old-domain.com/user/view/123?company_id=ABC
will be redirected to
new-domain.com/user/view/123?company_id=ABC
.
But, I need all requests to these URLS to not be redirected.
old-domain.com/user/view/123?company_id=XYZ
old-domain.com/user/view/123
old-domain.com/other/api/as/well
Here is what I have made so far.
if ($arg_company_id = ABC) {
return 302 https://new-domain.com$request_uri;
}
All is working well but I face another issue. I also need to redirect all POST requests as well.
old-domain.com/user/update/123?company_id=XYZ
POST variables : name=John, phone_number=555123
The above request will only redirect the request, but not the POST data (the name=John, phone_number=555123
data is not sent to the new domain).
I have read about proxy_pass
but that way is not working for a different domain.
I hope I can get some enlightment. Thank you.
UPDATE :
Thank you to Ivan's answer. I modify my conf to this (nginx doesn't support nested-if):
set $RR "";
if ($arg_company_id = ABC) {
set $RR R;
}
if ($request_method = GET) {
set $RR "${RR}G";
}
if ($request_method = POST) {
set $RR "${RR}P";
}
if ($RR = RG){
return 302 https://new-domain.com$request_uri;
}
if ($RR = RP) {
return 307 https://new-domain$request_uri;
}
Best Answer
Try HTTP 307 redirect, it should keep POST request body: