Remove query string on rewrite

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?

Thank you!

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,118933,118933#msg-118933

2010/8/12 celevra [email protected]:

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;
}
}

Hello!

On Thu, Aug 12, 2010 at 02:32:56AM -0400, celevra wrote:

permanent;
}
}

But the query string remains, and the final location is
/author/xxx?name=xxx

How can i remove the unneeded query string?

Add trailing “?” to replacement string.

http://wiki.nginx.org/NginxHttpRewriteModule#rewrite

Maxim D.

Thank Maxim D. for take care, I noted, and try now
tyanhly

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,118933,222092#msg-222092

Thank Mikhail Mazursky, I try now.

tyanhly

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,118933,222091#msg-222091

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs