How to do substitions (like perl s/// operator) in rewrites?

bartschipper nginx-forum at nginx.us
Tue Jan 12 00:18:17 MSK 2010


Issue solved!

I followed Maxim's advice and compiled embedded perl in. However, I found the configuration not as trivial as Maxim claimed: embedded perl documentation is scarce and examples are rare.

The following configuration solved it for me and  it may serve as an example for others of using the map module together with embedded perl:

http {
...

# use the map module to include a list with keys and corresponding article-ids 
  map $uri $old-class-url {
    include /etc/nginx/rewrites/old-class-urls.txt; # (see the first post for a sample of the contents)
  }

# lower case uri's file name part and remove dashes with perl. Return result to nginx variable $old_uri
  perl_set $old_uri 'sub {
    my $r = shift;
    my $uri = $r->uri;
    if($uri =~ /\/([^\/]+)\.htm$/) {
        $uri = lc($1);
        $uri =~ s/-//g;
    }
    return $uri;
  }';
...
  server {
...
    location ~* ^/News/Articlepage-News/.*htm$ {
# rewrite $uri to variable $old_uri
      rewrite ^ $old_uri ;
# return the article-id in the rewrite if $uri appears in the map
      if ($old-class-url) {
        rewrite  ^    /old-class-url.php?articleid=$old-class-url   permanent;
      }
    }
...
  }
}


I welcome any suggestion for a simpler solution.

Bart Schipper

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,39425,39944#msg-39944




More information about the nginx mailing list