# Adding query string to redirect?

**URL:** <https://www.ruby-forum.com/t/adding-query-string-to-redirect/235584>\
**Category:** NGINX\
**Created:** [July 1, 2013, 11:41pm UTC](https://www.ruby-forum.com/t/adding-query-string-to-redirect/235584 "2013-07-01T23:41:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alcina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@alcina](https://www.ruby-forum.com/u/alcina)\
**Post date:** [July 1, 2013, 11:41pm UTC](https://www.ruby-forum.com/t/adding-query-string-to-redirect/235584/1 "2013-07-01T23:41:20Z")

</div>

we’d like to add onto the query string an identifier of the nginx server

something like:

return 301 https://$host$request\_uri?source=server1 ;

the problem is that we can’t figure out how to make this work correctly  
when the url already contains query strings.

Example:  
return 301 https://$host$request\_uri?source=server1 ;  
Good!  
in /foo.bar  
out /foo.bar?source=server1  
Bad!  
in /foo.bar?a=1  
out /foo.bar?a=1?source=server1

How can we get this?

```
in /foo.bar
out /foo.bar?source=server1

in /foo.bar?a=1
out /foo.bar?a=1&source=server1
```

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [July 2, 2013, 12:11am UTC](https://www.ruby-forum.com/t/adding-query-string-to-redirect/235584/2 "2013-07-02T00:11:43Z")

</div>

At the location/server level try:

if ($is\_args) {  
return 301 https://$host$request\_uri&source=server1;  
}

## Goes here if the above is not chosen.

return 301 https://$host$uri?source=server1 ;

----appa

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [July 2, 2013, 12:14am UTC](https://www.ruby-forum.com/t/adding-query-string-to-redirect/235584/3 "2013-07-02T00:14:38Z")

</div>

On Mon, Jul 01, 2013 at 05:18:47PM -0400, Jonathan V. wrote:

Hi there,

> the problem is that we can’t figure out how to make this work correctly when the  
> url already contains query strings.

If the format difference between $request\_uri and $uri is not important  
to you in this case, and if the order of the arguments is not important,  
then replacing

$request\_uri

with

$uri?source=server1&$args

might be enough.

See [Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#variables)  
for what the different variables mean.

If those conditions don’t hold, then you may need to set your extra bit  
to start with ? or & depending on the existence of $args or of $is\_args,  
which you could do in a map.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
