Apply URL encoding during rewrite rule

Ed W lists at wildgooses.com
Wed Feb 1 20:59:41 UTC 2012


On 01/02/2012 19:38, António P. P. Almeida wrote:
> On 1 Fev 2012 19h24 WET, lists at wildgooses.com 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.
>
>

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



More information about the nginx mailing list