我使用mod_rewrite
使用apache httpd 2.2.15(Unix)我有这个改写:
RewriteEngine On
RewriteLogLevel 7
RewriteMap encode-map prg:/var/www/centos6.local/encode.pl
RewriteLog "/var/log/httpd/centos6.local-rewrite_log"
RewriteCond %{QUERY_STRING} CID=(.*)
RewriteRule .* - [L,CO=NDD_COOKIE:${encode-map:%1}:ndipiazza.test.local:50000]
这样做的目的是检测您是否在查询字符串中的任何位置找到CID = xyz。如果这样做,请创建一个带有xyz的cookie NDD_COOKIE,该xyz由自定义perl脚本编码/var/www/centos6.local/encode.pl
这只设置一次cookie,然后在服务器完全重启之前不再设置cookie。
0)设置hosts文件,使ndpiazza.test.local指向192.168.2.7
1)启动http://ndipiazza.test.local/test.html?CID=test123
结果:编码的cookie按预期编写
2)切换到新浏览器或清除浏览缓存。
3)启动http://ndipiazza.test.local/test.html?CID=new567
结果:没有写入cookie!
Here is a link to the encode.pl script
为什么会这样?
以下是http://ndipiazza.test.local/test.html?UID=sfsfsfd888m无效时的重写日志:
192.168.2.3 - - [28/Jun/2012:16:33:29 --0500] [ndipiazza.test.local/sid#7fc041c0c880][rid#7fc041d34608/initial] (2) init rewrite engine with requested uri /test.html
192.168.2.3 - - [28/Jun/2012:16:33:29 --0500] [ndipiazza.test.local/sid#7fc041c0c880][rid#7fc041d34608/initial] (3) applying pattern '.*' to uri '/test.html'
192.168.2.3 - - [28/Jun/2012:16:33:29 --0500] [ndipiazza.test.local/sid#7fc041c0c880][rid#7fc041d34608/initial] (4) RewriteCond: input='UID=sfsfsfd888' pattern='UID=(.*)' => matched
192.168.2.3 - - [28/Jun/2012:16:33:29 --0500] [ndipiazza.test.local/sid#7fc041c0c880][rid#7fc041d34608/initial] (5) map lookup OK: map=encode-map key=sfsfsfd888 -> val=
192.168.2.3 - - [28/Jun/2012:16:33:29 --0500] [ndipiazza.test.local/sid#7fc041c0c880][rid#7fc041d34608/initial] (5) setting cookie 'WS_CUID=ndipiazza.test.local; path=/; domain=50000'
192.168.2.3 - - [28/Jun/2012:16:33:29 --0500] [ndipiazza.test.local/sid#7fc041c0c880][rid#7fc041d34608/initial] (1) pass through /test.html
答案 0 :(得分:1)
如http://httpd.apache.org/docs/current/rewrite/rewritemap.html#prg中所述,该脚本仅在启动Apache HTTPD时调用。应该为每个请求返回不同值的RewriteMap应该使用dbd处理程序(仅在加载和配置mod_dbd时才可用)。
您可以将脚本作为CGI运行,甚至可以使用http://perldoc.perl.org/CGI/Cookie.html从perl代码设置cookie,或者使用shell_exec从PHP调用脚本,并使用该函数返回的值设置cookie。
答案 1 :(得分:0)
答案:系统只会打开一次encode.pl。预计会持续不断地监听标准输入。
但你仍然可以使用prg rewritemap来做到这一点。
我缺少的一件大事是我在perl中需要一个while循环。
以下是我最终解决的问题:
httpd.conf条目:
RewriteEngine On
RewriteLogLevel 6
RewriteLog "/var/log/httpd/centos6.local-rewrite_log"
RewriteCond %{QUERY_STRING} UID=(.*)
RewriteMap encode-map prg:/var/www/centos6.local/encode.pl
RewriteRule .* - [L,CO=NDD_COOKIE:${encode-map:%1}:ndipiazza.test.local:50000]
<强> encode.pl:强>
#!/usr/bin/perl
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
$| = 1; # Turn off I/O buffering
while (<STDIN>) {
s/-/0/g; # Replace dashes with 0's
$a = $_;
$s = '';
for ($i=0; $i<length($a); ++$i) {
$c = substr($a, $i, 1);
if ($c eq 'Z') {
$s = $s . 'a';
} elsif ($c eq 'z') {
$s = $s . 'A';
} elsif ($c eq '9') {
$s = $s . '0';
} else {
$s = $s . chr(ord($c)+1);
}
}
$s = scalar reverse trim($s);
print "$s\n";
}