http://www.domolo.com/tianchunfeng
http://blog.csdn.net/accesine960
http://360.yahoo.com/tcf960
http://www.donews.net/accesine 你想把上面这些页面通过一个统一的的方式来访问:
比如是:
http://www.domolo.com/tianchunfeng ------> http://www.domolo.com/tianchunfeng
http://blog.csdn.net/accesine960 ------> http://www.domolo.com/accesine960
http://360.yahoo.com/tcf960 ------> http://www.domolo.com/tcf960
http://www.donews.net/accesine ------> http://www.domolo.com/accesine 那么可以用如下方式实现:
1、建立一个 影射文件:domolo.map 并把它放在:/APACHE_HOME/apache/conf目录下:
tianchunfeng http://www.domolo.com/tianchunfeng
accesine960 http://www.domolo.com/accesine960
tcf960 http://www.domolo.com/tcf960
accesine http://www.domolo.com/accesine
2、RewriteMap fishmap txt:/
APACHE_HOME/apache/conf/domolo.map
3、RewriteEngine On
RewriteRule ^/myhomepage/(.*) ${domolo:$1} [R]
这样当用户访问: http://domolo/myhomepage/tianchunfeng, 的时候实际转向到 http://www.domolo.com/tianchunfeng。
这个规则挺方便的吧。
在我们访问Web页面的时候偶尔会碰到404错误,就是页面无法访问,这个也可以通过 url Rewrite来实现:
RewriteRule ^/myhomepage/(.*) ${domolo:$1|http://www.domolo.com/} [R]
上面的例子是1对1的影射url rewrite,更复杂的可以用以下的方式来实现,比如:
#!/usr/bin/perl
$| = 1; # Turn off buffering
while (<STDIN>) {
s/-/_/g; # Replace - with _ globally
print $_;
}
上面的例子是把url中所有的符号“-”换成:“_”
我们把上面的perl保存成:dash3score.pl ,并做如下配置:
RewriteMap dash2score prg:/APACHE_HOME/conf/dash2score.pl
RewriteEngine On
RewriteRule (.*-.*) ${dash2score:$1} [PT]
一个更复杂的例子是把影射关系存储在数据库中,不妨看看下面的例子(配置省略):
#!/usr/bin/perl
use DBI;
$|=1;
my $dbh = DBI->connect('DBI:mysql:wordpress:dbserver', 'username', 'password');
my $sth = $dbh->prepare("SELECT ID FROM wp_posts WHERE post_name = ?");
my $ID;
# Rewrite permalink style links to article IDs
while (my $post_name = <STDIN>) {
chomp $post_name;
$sth->execute($post_name);
$sth->bind_columns(\$ID);
$sth->fetch;
print "/domolo/index.php?p=$ID\n";
}
相关链接:
多么乐
ApacheCookbook
田春峰
20050515