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:

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:

That is incorrectly rewritten as:

How can I instead redirect the user to:

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.