# Rewrite with $request\_uri auto appends args

**URL:** <https://www.ruby-forum.com/t/rewrite-with-request-uri-auto-appends-args/198698>\
**Category:** NGINX\
**Created:** [November 5, 2010, 6:09pm UTC](https://www.ruby-forum.com/t/rewrite-with-request-uri-auto-appends-args/198698 "2010-11-05T18:09:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![okkezSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/85f322/32.png) [@okkezSS](https://www.ruby-forum.com/u/okkezSS)\
**Post date:** [November 5, 2010, 6:09pm UTC](https://www.ruby-forum.com/t/rewrite-with-request-uri-auto-appends-args/198698/1 "2010-11-05T18:09:43Z")

</div>

I’ve been using the following code to redirect non-www to www-refixed  
domains:

```auto
server {
    server_name example.com;
    rewrite ^ http://www.example.com$request_uri permanent;
}
```

However, I just realized that when there is a query string, the  
resulting URI has the query string twice. For example:

```auto
http://example.com/test.html?a=1&b=2
```

…is rewritten as:

```auto
http://www.example.com/test.html?a=1&b=2?a=1&b=2
```

Is this the correct behavior?

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  
supposed to auto-append the arguments unless I explicitly put arguments  
on the replacement side.

I’m running Nginx 0.8.52.

Posted at Nginx Forum:

> **[rewrite with $request\_uri auto appends args](https://forum.nginx.org/read.php?2,148047,148047#msg-148047)**
>
> 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

---

<div class="post-metadata">

**Author:** ![NCRonB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ad7895/32.png) [@NCRonB](https://www.ruby-forum.com/u/NCRonB)\
**Post date:** [November 5, 2010, 6:26pm UTC](https://www.ruby-forum.com/t/rewrite-with-request-uri-auto-appends-args/198698/2 "2010-11-05T18:26:40Z")

</div>

Hello!

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

> However, I just realized that when there is a query string, the  
> resulting URI has the query string twice. For example:
> 
> ```auto
> http://example.com/test.html?a=1&b=2
> ```
> 
> …is rewritten as:
> 
> ```auto
> http://www.example.com/test.html?a=1&b=2?a=1&b=2
> ```
> 
> 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:

> **[Converting rewrite rules](http://nginx.org/en/docs/http/converting_rewrite_rules.html)**

: server {  
: listen 80;  
: server\_name [nginx.org](http://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 D.

---

<div class="post-metadata">

**Author:** ![NCRonB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ad7895/32.png) [@NCRonB](https://www.ruby-forum.com/u/NCRonB)\
**Post date:** [November 5, 2010, 6:50pm UTC](https://www.ruby-forum.com/t/rewrite-with-request-uri-auto-appends-args/198698/3 "2010-11-05T18:50:32Z")

</div>

> Classic “redirect everything” example contains  
> ‘?’. See e.g. here:
> 
> [Converting rewrite rules](http://nginx.org/en/docs/http/converting_rewrite_rules.html)
> 
> : server {  
> : listen 80;  
> : server\_name [nginx.org](http://nginx.org);  
> : rewrite ^ http://www.nginx.org$request\_uri?;  
> : }

Well, it sure does. [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/Pitfalls#Taxing_Rewrites) does  
not show the ‘?’ and I (think) I’ve read many other configurations that  
don’t have it, but perhaps I’m just getting old.

> 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 ‘?’.

Got it. However, if you use $request\_uri, doesn’t that always preserve  
the original args by itself? I’m having trouble thinking of an example  
where you would NOT want a question mark after $request\_uri in a rewrite  
statement.

Thanks!

–  
Ron

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,148047,148067#msg-148067](http://forum.nginx.org/read.php?2,148047,148067#msg-148067)
