Welcome! Log In Create A New Profile

Advanced

Re: rewrite with $request_uri auto appends args

Maxim Dounin
November 05, 2010 01:30PM
Hello!

On Fri, Nov 05, 2010 at 01:08:47PM -0400, NCRonB wrote:

> I've been using the following code to redirect non-www to www-refixed
> domains:
>
> [code]
> server {
> server_name example.com;
> rewrite ^ http://www.example.com$request_uri permanent;
> }
> [/code]
>
> However, I just realized that when there is a query string, the
> resulting URI has the query string twice. For example:
>
> [code]http://example.com/test.html?a=1&b=2[/code]
>
> ...is rewritten as:
>
> [code]http://www.example.com/test.html?a=1&b=2?a=1&b=2[/code]
>
> Is this the correct behavior?

Yes. Variable $request_uri contains args, while rewrite without
trailing '?' will add args.

> I can add a '?' to the end of $request_uri to make it work as expected,
> but I've never seen that in any examples, and I didn't think rewrite was

Classic "redirect everything" example contains '?'. See e.g.
here:

http://nginx.org/en/docs/http/converting_rewrite_rules.html

: server {
: listen 80;
: server_name nginx.org;
: rewrite ^ http://www.nginx.org$request_uri?;
: }

> supposed to auto-append the arguments unless I explicitly put arguments
> on the replacement side.

rewrite is expected to preserve original args in all cases (either
preserve as is, or append if you added other arguments) unless you
specified trailing '?'.

Maxim Dounin

_______________________________________________
nginx mailing list
nginx@nginx.org
http://nginx.org/mailman/listinfo/nginx
Subject Author Posted

rewrite with $request_uri auto appends args

NCRonB November 05, 2010 01:08PM

Re: rewrite with $request_uri auto appends args

Maxim Dounin November 05, 2010 01:30PM

Re: rewrite with $request_uri auto appends args

NCRonB November 05, 2010 01:49PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 323
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready