|
How to do substitions (like perl s/// operator) in rewrites? January 10, 2010 09:08AM | Registered: 6 months ago Posts: 3 |
# old-class-urls.txt: (first two commented lines are not actually in the file) # all lowercase title without spaces and hyphens article-id thisisthebestnewsever 123456; ...In apache I used a rewritemap to rewrite these urls:
# httpd.conf: ... RewriteMap articles prg:/etc/httpd/rewrites/old-class-article-urls.pl ...
# old-class-article-urls.pl:
#!/usr/bin/perl
$| = 1;
###############################################
# code to be executed at startup of webserver #
###############################################
open(TEXTFILE,"</etc/httpd/rewrites/old-class-urls.txt"); # loading the rewrite map in memory
@lines = <TEXTFILE>;
close TEXTFILE;
foreach $line (@lines) { # load the data in an associative array for fast lookup
($keyword,$article_id) = split(/\s+/,$line);
$keys{$keyword} = $article_id;
}
##########################################
# code to be once every URL is requested #
##########################################
while (<STDIN>) {
$url = $_;
chomp($url);
if($url =~ /\/([^\/]+)\.htm$/) { # a match could be made
$keyword = lc($1);
$keyword =~ s/-//g;
if($keys{$keyword}) {
print 'old-class-url.php?articleid=' . $keys{$keyword}."\n";
next;
}
}
print "Not found\n"; # no match could be made
}
I was hoping something similar or even easier could be done with Nginx:
map $uri $old-class-url {
include /etc/nginx/rewrites/old-class-urls.txt;
}
...
server {
...
location ~* ^/News/Articlepage-News/.*htm {
rewrite ^/News/Articlepage-News/(.*)htm $1 ;
###
### change $uri to lowercase and remove the hyphens...
### I am looking for something equivalent like in perl:
### s/-//g;
### s/.*/\L{$1}/;
###
if ($old-class-url) {
rewrite ^ /old-class-url.php?articleid=$old-class-url permanent;
}
}
}Maxim Dounin
Re: How to do substitions (like perl s/// operator) in rewrites? January 10, 2010 09:32AM |
|
Re: How to do substitions (like perl s/// operator) in rewrites? January 10, 2010 12:09PM | Registered: 8 months ago Posts: 12 |
|
Re: How to do substitions (like perl s/// operator) in rewrites? January 11, 2010 03:28AM | Registered: 6 months ago Posts: 3 |
thisisthebestnewsever 123456; expertsadvicetoeatmorefish 123457; ...I can not think of an automated way to convert this to:
this-is-the-best-news-ever 123456; thisisthebestnewsever 123456; experts-advice-to-eat-more-fish 123457; expertsadvicetoeatmorefish 123457; ...Knowing that the map module is case-insensitive may safe me from confusion in the future.
|
Re: How to do substitions (like perl s/// operator) in rewrites? January 11, 2010 04:18PM | Registered: 6 months ago Posts: 3 |
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;
}
}
...
}
}![]() | ![]() | ![]() | ![]() |



