Apply URL encoding during rewrite rule

Hi, I need to do a redirect and pass the old URL to the redirected
destination. How might I achieve this using nginx?

I tried a rewrite rule simply:
rewrite ^/(.*)
$scheme://$server_addr/?redirect_to=$scheme://$host$request_uri
redirect;

However, this simply appends the old url verbatim on the end of the
?redirect_to parameter

I tried a few other variables, but I don’t see any way to ask Nginx to
url-encode something for me?

Any other thoughts on ways to achieve the desired effect? The
redirected to destination is a web app under my control - we could proxy
the initial request to the web app and have it generate the redirect,
but I was hoping to decouple things

Thanks for any thoughts

Ed W

On 1 Fev 2012 19h24 WET, [email protected] wrote:

Hi, I need to do a redirect and pass the old URL to the redirected
destination. How might I achieve this using nginx?

I tried a rewrite rule simply: rewrite ^/(.*)
$scheme://$server_addr/?redirect_to=$scheme://$host$request_uri
redirect;

To escape do a rewrite with a capture. Above you’re capturing but not
using the numeric group $1. Try:

rewrite ^/(.*)$ $scheme://$server_addr/?redirect_to=$scheme://$host/$1
redirect.

— appa

On 01/02/2012 19:38, Antnio P. P. Almeida wrote:

rewrite ^/(.*)$ $scheme://$server_addr/?redirect_to=$scheme://$host/$1 redirect.

OK, that nearly works, but it adds an extra & when nginx appends the
existing params.

This is closer:

 rewrite ^(.*)$

$scheme://$server_addr/?redirect_to=$scheme%3A%2F%2F$host$1%3F$args?
redirect;

However, for some reason the $1 is still decoded ? From reading
various past threads on this, I thought that a regexp would grab the
still encoded $uri part?

So I don’t see any difference between using $1 or $uri to capture the
uri, and in both cases it will decode a uri such as:

 http://www/as%2Fdf

as

 http://www/as/df

Am I missing something that is causing this to happen? How to grab the
$uri without decoding?

Nearly there! Thanks

Ed W