Faisal I
Faisal I

Reputation: 379

Nginx Redirect depending on URL Param

I am new at nginx and need help with redirect (basically I am a Java Dev), so here is what I need suggestions for : I have a url https://www.mysite.com/abc?server=10.2.2.10

I want nginx to check if server param is set then it should rewrite url to

https://10.2.2.10/abc

here is what I have tried but it isn't working:

location /abc/{  # I have also tried with /abc
    if ($arg_server != ""){
        rewrite https://$arg_server/$1 permanent;
    }
}

any suggestions please? Thank you for any helps guys

best Regards Sajid

Upvotes: 1

Views: 1346

Answers (1)

Mohammad AbuShady
Mohammad AbuShady

Reputation: 42899

Actually the rewrite seems wrong, try replacing the rewrite line with this

if ($arg_server != ""){
    return 301 https://$arg_server/$1;
}

Adding a ^ to your rewrite probably would have worked but it's better to use return instead of rewrite here's why

Also I believe running a simple if($arg_server) would work without the != part, you can try it, Not sure though if it would work if it was empty string, like example.com?server=, if it does then just ignore what i said

Upvotes: 1

Related Questions