9

I want to redirect anything that comes directly to my server with perticuler query string to other location in same domain.

If user comes to

http://www.mydomain.com/?abc=js9sd70s

I want to redirect it to

http://www.mydomain.com/otherpath/?abc=js9sd70s

the query string ?abc=js9sd70s should be the same to new url.

Please suggest nginx config file.

I have tried most of the alternative for below code. by keeping '\' before ? and = sign.

        location ~ /?abc=.* {
                rewrite ^/(.*)$ http://www.mydomain.com/otherpath/$1 permanent;
        }

Please suggest me this location change.

1 Answer 1

22

Short answer, try this configuration:

location = / {
    if ( $arg_abc ) {
        rewrite ^ /otherpath/ permanent;
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Yes it redirected www.mydomain.com/?abc=js9sd70s to www.mydomain.com/otherpath/?abc=js9sd70s but now It is trying to redirect www.mydomain.com/otherpath/?abc=js9sd70s again and again, and it is not getting completed. Thanks for this help
can I put special check for ?abc is at domain root, then and only this redirection rule applies? like in this case www.mydomain.com/?abc=js9sd70s should redirected but www.mydomain.com/otherpath/?abc=js9sd70s should not be.
The rule above working only in domain root, use: location = / { documentation: wiki.nginx.org/HttpCoreModule#location
ok, Do I need to separate location /otherpath/ from location / in nginx config file? till now I have just one segment location / { #rewrite code } in config.
Please! Be carefull! location / and location = / it's a different locations! Read documentation first.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.