Search and replace query string

Igor Sysoev is at rambler-co.ru
Tue Aug 7 00:40:43 MSD 2007


On Mon, Aug 06, 2007 at 12:12:11PM -0700, Chuck Vose wrote:

> Since I didn't get a response I thought I would try a second
> clarifying email and see if it makes more sense to the people that
> know.
> 
> We're trying to take urls like this:
> example.com/api?test1=test2&test3=test4
> and turn them into urls like this:
> example.com/api/test1/test2/test3/test4
> 
> This is easy if there are a fixed number of variables in the query
> string but we don't have that. Sometimes we have /test1=test2 but
> sometimes it's /test1=test2&test3=test4&test5=test6&test7=test8, etc.
> I think the most we'll ever have is about 5 deep but I'm not sure.
> 
> So is there any way to have a search and replace regex? I couldn't
> find any examples of s///g regex's in the documentation. Do I need to
> get mod_perl involved or do we need to move to apache? Maybe it's just
> time to reassess but this seems like an awesome solution if we can get
> it rolling.
> 
> > Here's the code I've tried to create.
> >
> > if ($query_string ~ ^(s/(\?|&|=)(?:)/\//g)$) {
> >   set $new_url $1;
> >   rewrite ^(.*)$ $new_url.html break;
> > }

No, nginx does not allow "s///". Currently the only way to do it
inside nginx is ngx_http_perl_module variable:

http {

     perl_set  $new_url  'sub {
          my $r = shift;
          my $a = $r->args;
          $a =~ s/(&|=)/\//g;
          return $a;
     }';

     server {

         location / {
              rewrite  ^(.+)$  $1$new_url.html  break;
              ...
         }


-- 
Igor Sysoev
http://sysoev.ru/en/





More information about the nginx mailing list