查看文章 |
Apache Rewrite 规则的常见应用(三)
2008-03-09 10:47
RewriteRule
Syntax: RewriteRule Pattern Substitution [flags] 一条RewriteRule指令,定义一条重写规则,规则间的顺序非常重要。对Apache1.2及以后的版本,模板(pattern)是一个POSIX正则式,用以匹配当前的URL。当前的URL不一定是用记最初提交的URL,因为可能用一些规则在此规则前已经对URL进行了处理。 对mod_rewrite来说,!是个合法的模板前缀,表示“非”的意思,这对描述“不满足某种匹配条件”的情况非常方便,或用作最后一条默认规则。当使用!时,不能在模板中有分组的通配符,也不能做后向引用。 当匹配成功后,Substitution会被用来替换相应的匹配,它除了可以是普通的字符串以外,还可以包括:
这些特殊内容的扩展,按上述顺序进行。
一个URL的全部相关部分都会被Substitution替换,而且这个替换过程会一直持续到所有的规则都被执行完,除非明确地用L标志中断处理过程。 当susbstitution有”-”前缀时,表示不进行替换,只做匹配检查。 利用RewriteRule,可定义含有请求串(Query String)的URL,此时只需在Sustitution中加入一个?,表示此后的内容放入QUERY_STRING变量中。如果要清空一个QUERY_STRING变量,只需要以?结束Substitution串即可。 如果给一个Substitution增加一个http://thishost[:port]的前缀,则mod_rewrite会自动将此前缀去掉。因此,利用http://thisthost做一个无条件的重定向到自己,将难以奏效。要实现这种效果,必须使用R标志。 Flags是可选参数,当有多个标志同时出现时,彼此间以逗号分隔。
注意:一定不要忘记,在服务器范围内的配置文件中,模板(pattern)用以匹配整个URL;而在目录范围内的配置文件中,目录前缀总是被自动去掉后再进行模板匹配的,且在替换完成后自动再加上这个前缀。这个功能对很多种类的重写是非常重要的,因为如果没有去前缀,则要进行父目录的匹配,而父目录的信息并不是总能得到的。一个例外是,当substitution中有http://打头时,则不再自动增加前缀了,如果P标志出现,则会强制转向代理。 注意:如果要在某个目录范围内启动重写引擎,则需要在相应的目录配置文件中设置“RewriteEngine on”,且目录的“Options FollowSymLinks”必须设置。如果管理员由于安全原因没有打开FollowSymLinks,则不能使用重写引擎。 注:顺序
下表从最高优先级到最低优先级列出各种正则表达式操作符的优先权顺序:
操作符描述 \ 转义符 (), (?:), (?=), [] 圆括号和方括号 *, +, ?, {n}, {n,}, {n,m} 限定符 ^, $, \anymetacharacter 位置和顺序 | “或”操作 RewriteRule news/(\d+)\.html news\.php\?id=$1 [N,L]
这样就实现了将http://localhost/news/1000.html 解析为 http://localhost/news.php?id=1000的功能
http://211.100.227.106:8080/wp-cp-m7/index.jsp?mod=free&act=watch&cid=714
index/[a-zA-Z]/[a-zA-Z]/(\d+)_(\d+)\.html index\.jsp\?mod=$1&act=$2&cid=
RewriteRule ^(power([^/]*))/([^/]+)\.htm(l?)$ series\.php\?fclassname=$1&fb_babrand=$3 [NC]
$ 匹配输入字符串的结束位置。如
将URL http://host/test.jps?id=1 重定向为 http://host/1.html
将URL http://host/conent.jsp?id=1&id2=3 重定向为 http://host/1_3.html
RewriteEngine on
RewriteRule /([0-9]+).html /test.jsp?id=$1 [PT]
RewriteRule /([0-9]+)_([0-9]+).html /content.jsp?id=$1&id2=$2 [PT]
index.jsp?lid=2&mod=free&act=watch&cid=714&mid=112312&type=adlska
index/lang/mod/act/type/cid_mid.html
RewriteRule index/(\d+\.?\d*)/(a-z)/(a-z)/(a-z)/([0-9]+)_([0-9]+).html /index.jsp?lid=$1&mod=&2&act=$3&type=$4&cid=$5&mid=$6 [PT]
RewriteRule index/(a-z)/(a-z)/(a-z)/([0-9]+)_([0-9]+).html /index.jsp?mod=&1&act=$2&type=$3&cid=$4&mid=$5 [PT]
RewriteRule /(a-z)/(a-z)/([0-9]+)_([0-9]+).html /?mod=&1&act=$2&cid=$3&mid=$4 [PT]
index.jsp?mod=free&act=watch&cid=714
index.jsp?mod=free&act=watch
index.jsp?mod=free
RewriteRule ^([^/]+)/([^/]+)/(\d+)_(\d+).html$ /index.jsp?mod=$1&act=$2&cid=$3&mid=$4
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/(\d+)_(\d+).html$ /index.jsp?lid=$1&mod=$2&act=$3&cid=$4&mid=$5
RewriteRule ^([^/]+)/([^/]+)/(\d+).html$ /index.jsp?mod=$1&act=$2&cid=$3
RewriteRule ^([^/]+)/([^/]+)/(\d+)/(\d+).html$ /index.jsp?lid=$1&mod=$2&act=$3&cid=$4
RewriteRule ^(\d+)/(\d+).html$ /index.jsp?mod=$1&act=$2
RewriteRule ^(\d+)/([^/]+)/([^/]+).html$ /index.jsp?lid=$1&mod=$2&act=$3
RewriteRule ^([^/]+).html$ /index.jsp?mod=$1
RewriteRule ^(\d+)/([^/]).html$ /index.jsp?lid=$1&mod=$2 |
最近读者:

