# Encode url

**URL:** <https://www.ruby-forum.com/t/encode-url/199685>\
**Category:** NGINX\
**Created:** [November 26, 2010, 10:52am UTC](https://www.ruby-forum.com/t/encode-url/199685 "2010-11-26T10:52:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [November 26, 2010, 10:52am UTC](https://www.ruby-forum.com/t/encode-url/199685/1 "2010-11-26T10:52:25Z")

</div>

Hi,

I’m writing my first nginx module. The module at some point uses the  
“Location” header to redirect to user to a landing page. The redirected  
url has many arguments which could potentially contain spaces, slashes  
or any other non-alphabetic chars.

Is there a function that’ll escape then and convert them to %20, %2f and  
so on?

Thanks.

Posted at Nginx Forum:

> **[encode url](https://forum.nginx.org/read.php?2,153434,153434#msg-153434)**
>
> Hi, I'm writing my first nginx module. The module at some point uses the "Location" header to redirect to user to a landing page. The redirected url has many arguments which could potentially contain spaces, slashes or any other...

---

<div class="post-metadata">

**Author:** ![pattryn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6bbea6/32.png) [@pattryn](https://www.ruby-forum.com/u/pattryn)\
**Post date:** [January 18, 2011, 8:53am UTC](https://www.ruby-forum.com/t/encode-url/199685/2 "2011-01-18T08:53:50Z")

</div>

Hi,

I really need the same for rewrite and variables like $http\_referer or  
$request\_uri.

There were several questions regarding that in the past, but I can’t  
find a solution.

An example:  
[code]

# url rewriting

```
location ~ \.htm$ {
  rewrite ^/([a-z0-9_]+)\.htm$ /$1.php?uri=$request_uri break;
  proxy_next_upstream error timeout http_500 http_503 http_404

```

invalid\_header;  
proxy\_pass [http://backend](http://backend);  
}  
[/code]

In some rules $request\_uri contains arguments with ampersand and as it  
isn’t escaped, those won’t be part of the “uri”-var.

I really like to have a function like url\_encode() or something similar  
to str\_replace().

regards,  
Marc

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,153434,167140#msg-167140](http://forum.nginx.org/read.php?2,153434,167140#msg-167140)

---

<div class="post-metadata">

**Author:** ![pattryn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6bbea6/32.png) [@pattryn](https://www.ruby-forum.com/u/pattryn)\
**Post date:** [January 24, 2011, 10:08am UTC](https://www.ruby-forum.com/t/encode-url/199685/3 "2011-01-24T10:08:50Z")

</div>

Same problem here:

> **[Problem with the lack of url encoding in the rewrite result url](https://forum.nginx.org/read.php?2,34381)**
>
> Hi everyone! Recently we've moved from lighttpd to nginx and I've discovered a problem with the different rewrite handling in nginx. The rule: rewrite ^/download/(\[^/\]+)/(.+)/$ /index.php/download/?hash=$1&filename=$2 last; w

Posted at Nginx Forum:

> **[Re: encode url](https://forum.nginx.org/read.php?2,153434,169019#msg-169019)**
>
> Hi, I'm writing my first nginx module. The module at some point uses the "Location" header to redirect to user to a landing page. The redirected url has many arguments which could potentially contain spaces, slashes or any other...
