Decode url in rewrite

pedroaxl nginx-forum at
Sat Sep 10 00:21:59 UTC 2011

Hey guys,

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

Users would access a link like this:

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;

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

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;


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?


Posted at Nginx Forum:,215047,215047#msg-215047

More information about the nginx mailing list