Nignx Redirect rule to a different url based on a query

Geo asked:

We have a wordpress installation and need to redirect lost password link to another different url for this we trying to setup a redirect rule but this is not working. Can anyone please help us.

location / {
proxy_pass      http://localhost;

location wp-login.php?action=lostpassword
rewrite     ^(.*) $1 permanent;

But login url too (wp-login.php?action=login) is redirecting to

We need to redirect only the url wp-login.php?action=lostpassword to other and all other url including wp-login.php?action=login need to proxypass

Can anyone please help us with the correct configuration.


My answer:

Don’t use a location for this at all. It doesn’t match on query string arguments.

Instead, just check the argument and request URI directly. For example:

location / {
    if ($arg_action = "lostpassword") {
        rewrite /wp-login.php permanent;

    # everything else...

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.