# Apply URL encoding during rewrite rule

**URL:** <https://www.ruby-forum.com/t/apply-url-encoding-during-rewrite-rule/216333>\
**Category:** NGINX\
**Created:** [February 1, 2012, 8:25pm UTC](https://www.ruby-forum.com/t/apply-url-encoding-during-rewrite-rule/216333 "2012-02-01T20:25:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 1, 2012, 8:25pm UTC](https://www.ruby-forum.com/t/apply-url-encoding-during-rewrite-rule/216333/1 "2012-02-01T20:25:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ewildgoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewildgoose](https://www.ruby-forum.com/u/ewildgoose)\
**Post date:** [February 1, 2012, 8:39pm UTC](https://www.ruby-forum.com/t/apply-url-encoding-during-rewrite-rule/216333/2 "2012-02-01T20:39:36Z")

</div>

On 1 Fev 2012 19h24 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![ewildgoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewildgoose](https://www.ruby-forum.com/u/ewildgoose)\
**Post date:** [February 1, 2012, 10:00pm UTC](https://www.ruby-forum.com/t/apply-url-encoding-during-rewrite-rule/216333/3 "2012-02-01T22:00:07Z")

</div>

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
