Welcome! Log In Create A New Profile

Advanced

Decode url in rewrite

September 09, 2011 08:21PM
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 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
Subject Author Posted

Decode url in rewrite

pedroaxl September 09, 2011 08:21PM

Re: Decode url in rewrite

pedroaxl September 12, 2011 03:23PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 319
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready