实验过程如下:
rewrite模块都已经加载安装完毕,在一个虚拟主机里进行了如下配置
情况一:
ServerAdmin zhenggaoqi@gmail.com
DocumentRoot /var/www
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
RewriteEngine on
RewriteBase /
RewriteRule ^b(\/)?([a-z]*)?([0-9]{3,8})?$ index4.php?t
ime=%{TIME_SEC}&ss=$0&s1=$1&s2=$2&s3=$3&s4=$4&s5=$5
</Directory>
</VirtualHost>
访问地址:http://localhost/b/i2323
在http://localhost/index4.php中会把$_SERVER数组输出查看
结果如下:
[REMOTE_PORT] => 60254
[REDIRECT_QUERY_STRING] => time=08&ss=b/i2323&s1=/&s2=i&s3=2323&s4=&s5=
[REDIRECT_URL] => /b/i2323
[GATEWAY_INTERFACE] => CGI/1.1
[SERVER_PROTOCOL] => HTTP/1.1
[REQUEST_METHOD] => GET
[QUERY_STRING] => time=08&ss=b/i2323&s1=/&s2=i&s3=2323&s4=&s5=
[REQUEST_URI] => /b/i2323
[SCRIPT_NAME] => /index4.php
[PHP_SELF] => /index4.php
ServerAdmin zhenggaoqi@gmail.com
DocumentRoot /var/www
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
RewriteEngine on
RewriteBase /
RewriteRule ^([b]+)+(\/)?([a-z]*)?([0-9]{3,8})?$ index4.php?t
ime=%{TIME_SEC}&ss=$0&s1=$1&s2=$2&s3=$3&s4=$4&s5=$5
</Directory>
</VirtualHost>
访问地址:http://localhost/b/i2323
在http://localhost/index4.php中会把$_SERVER数组输出查看
结果如下:
[REMOTE_PORT] => 54105
[REDIRECT_QUERY_STRING] => time=53&ss=b/i2323&s1=b&s2=/&s3=i&s4=2323&s5=
[REDIRECT_URL] => /b/i2323
[GATEWAY_INTERFACE] => CGI/1.1
[SERVER_PROTOCOL] => HTTP/1.1
[REQUEST_METHOD] => GET
[QUERY_STRING] => time=53&ss=b/i2323&s1=b&s2=/&s3=i&s4=2323&s5=
[REQUEST_URI] => /b/i2323
[SCRIPT_NAME] => /index4.php
通过以后两种结果的对比我们可以发现,$0是捕获整个表达式的变量,然后子变量的分配是靠通过正则表达式中匹配过的片段集来实现的。
http://localhost/b/i2323
正则表达式一: ^b(\/)?([a-z]*)?([0-9]{3,8})?$
这种情况下,$1为匹配“(\/)?”的“/”,$2为"i"
正则表达式二: ^([b]+)+(\/)?([a-z]*)?([0-9]{3,8})?$
这种情况下,$1为匹配“([b]+)+”的“b”,$2为匹配“(\/)?”的“/”,$3为"i"
由此可见,我们可以通过适当的正当表达式来url替换url某些部分或者更改url的组成方式,来灵活的实现apache的url的rewrite。