Remove query string on rewrite

Mikhail Mazursky ash2kk at gmail.com
Thu Aug 12 13:58:05 MSD 2010


2010/8/12 celevra <nginx-forum at nginx.us>:
> Hi,
> I am trying to make a 301 redirect like this:
> /authors.php?name=xxx  ==> /author/xxx
>
> this config solves the problem partially -
>
>        location ~ ^/authors.php {
>                 if ($args ~ "name=(.+)" ) {
>                           set $arg_name $1;
>                          rewrite ^/authors\.php "/author/$arg_name"
> permanent;
>                }
>        }
>
> But the query string remains, and the final location is
> /author/xxx?name=xxx
>
> How can i remove the unneeded query string?

You should add a question mark at the end of the rewrite:

        location ~ ^/authors.php {
                 if ($args ~ "name=(.+)" ) {
                           set $arg_name $1;
                          rewrite ^/authors\.php "/author/$arg_name?" permanent;
                }
        }

there is a cleaner way to write this, but note that this way $arg_name
would not be URL encoded:

location ~ ^/authors.php {
       if ($arg_name) {
              rewrite ^/authors\.php /author/$arg_name? permanent;
       }
}



More information about the nginx mailing list