# Decode url in rewrite

**URL:** <https://www.ruby-forum.com/t/decode-url-in-rewrite/211663>\
**Category:** NGINX\
**Created:** [September 10, 2011, 2:22am UTC](https://www.ruby-forum.com/t/decode-url-in-rewrite/211663 "2011-09-10T02:22:28Z")\
**Posts on this page:** 2\
**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:** [September 10, 2011, 2:22am UTC](https://www.ruby-forum.com/t/decode-url-in-rewrite/211663/1 "2011-09-10T02:22:28Z")

</div>

Hey guys,

I am trying to solve a specific problem I have, but have not succeded.

Users would access a link like this:  
[http://mysite.com/24234234?key=3123&url=http%3A//mailee.me%3Fname%3Djohn%26code%3D123](http://mysite.com/24234234?key=3123&url=http%3A//mailee.me%3Fname%3Djohn%26code%3D123)

This is a normal url with some integer params and a url encoded param. I  
want nginx to automatically redirect to the url in the parameters. So I  
used the following code in the nginx.conf

```
      location /go/click {
              set $new_url $arg_url;
              set $args '';
              rewrite ^.*$ $new_url redirect;
      }

```

It works beautifully. The problem is that if the encoded url also have  
params, the client will receive the params encoded and it may not work  
in their system.

I try to use perl code to decode the url before redirecting, but it  
didnt work.

This is the code inside http context:  
perl\_modules perl/lib;  
perl\_require mailee.pm;

This is the code inside location context:  
location /go/click {  
perl mailee::decode;  
set $new\_url $arg\_url;  
#set $args ‘’;  
rewrite ^.\*$ $args redirect;  
}  
And the code in mailee.pm:

package mailee;  
use nginx;

sub decode {  
my $r = shift;  
$r-\>args =~ s/%3D/=/g;  
$r-\>args =~ s/%26/&/g;  
$r-\>args =~ s/%3F/?/g;  
$r-\>args =~ s/%3A/:/g;  
return OK;  
}

1;  
**END**

But it didnt work too, because I was not able to modify $args inside the  
perl function.

I am not sure if this is the best way to do it and if I am doing it  
right. Do you have any other solutions?

Thanks

Posted at Nginx Forum:

> **[Decode url in rewrite](https://forum.nginx.org/read.php?2,215047,215047#msg-215047)**
>
> Hey guys, I am trying to solve a specific problem I have, but have not succeded. Users would access a link like this: http://mysite.com/24234234?key=3123&url=http%3A//mailee.me%3Fname%3Djohn%26code%3D123 This is a normal url with some integer...

---

<div class="post-metadata">

**Author:** ![pedroaxl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/59ef9b/32.png) [@pedroaxl](https://www.ruby-forum.com/u/pedroaxl)\
**Post date:** [September 12, 2011, 9:23pm UTC](https://www.ruby-forum.com/t/decode-url-in-rewrite/211663/2 "2011-09-12T21:23:56Z")

</div>

Hi guys,

I solved my problem with set-misc module, now I use the set\_unescape\_uri  
to do it.

Posted at Nginx Forum:

> **[Re: Decode url in rewrite](https://forum.nginx.org/read.php?2,215047,215130#msg-215130)**
>
> Hey guys, I am trying to solve a specific problem I have, but have not succeded. Users would access a link like this: http://mysite.com/24234234?key=3123&url=http%3A//mailee.me%3Fname%3Djohn%26code%3D123 This is a normal url with some integer...
