Nginx Redirect depending on URL Param

Sajid

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

Mohammad AbuShady

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Redirect to a port number depending on the URL

Nginx redirect if url contain "/?&"

Nginx: URL Redirect and regex

How to redirect a url in NGINX

Reload URL with param depending on value of dropdown

React URL redirect on OnChange depending on state of checkbox

Trying to redirect a page depending on what is in the URL with Javascript

How to redirect to specific URL depending on password entered?

NGINX Redirect URL with Query Strings

How to redirect single url in nginx?

Nginx Subdomain redirect to static URL

Nginx - Wordpress redirect to default url

How to redirect invalid url to a valid url in nginx?

Redirect an Nginx Vhost/URL unless on a specific URL?

.htaccess how redirect url param to subdirectory

Dropdown options - redirect to custom url depending to selected option

Conditional redirect in .htaccess depending on the number of path segments in the URL

How to redirect to Tomcat depending on _escaped_fragment_ in the URL?

Redirect User to specific URL depending of selected dropdown during submit

I want to redirect to another page depending what on url paremeter

Nginx remove substring and add as url param

Nginx , save location path in url after redirect

Flask Redirect Creates Invalid URL with Nginx and Certbot

nginx : url's won't redirect

Nginx proxy redirect without changing url

Kubernetes nginx redirect to user-specified url

Redirect with nginx (remove substring from url)

Nginx redirect URL with specific query parameter

Nameserver always at the top in url bar in nginx (with no redirect)