Redirector - unescaping query parameters

I am trying to use nginx as a redirector that logs some data about the
request and then redirects the user to a user-specified URL.

This works well unless the user-specified URL contains query parameters.

For example, if this URL is requested:
http://my.site.com/redirect?p1=TEST&r=http%3A//www.new_site.com

I use this rewrite rule to redirect:

if ($arg_r ~* http.) {
rewrite ^(.
)$ $arg_r? redirect;
}

Any parameters in the user-specified URL are pre-escaped - notice that
the colon was replaced with %3A.

However, if the ‘r’ parameter that I redirect to also contains escaped
parameters, they do NOT get unescaped. For example, if the user requests
this URL:

http://my.site.com/redirect?p1=TEST&r=http%3A//my.site.jp%3Fparameter_1%3Dvalue_1%26parameter_2%3Dvalue_2

That is incorrectly rewritten as:
http://my.site.jp?parameter_1%3Dvalue_1%26parameter_2%3Dvalue_2

How can I instead redirect the user to:
http://my.site.jp?parameter_1=value_1&parameter_2=value_2

I do not know what parameters the user will be supplying in the
user-specified URL beforehand, so I can not extract them and rewrite
them manually. That is why they are encoded inside the ‘r’ parameter.

I’ve discovered it is possible to use the perl_set to unencode the ‘r’
parameter, but that seems messy.

I would be very grateful for any other suggestions.