RewriteRule ^/original/(.*).php /modified/$1.aspx
源:http://xxx/original/index.php
目标:http://xxx/modified/index.aspx
RewriteRule ^/dinoch/album/([^/]+)/([^/]+).(jpg|JPG|PNG) /chiesa/pics.aspx?d=$1&p=$2.$3
源:http://xxx/dinoch/album/30/1.jpg
目标:http://xxx/chiesa/pics.aspx?d=30&p=1.jpg
比较简单,主要还在于modifiers的功能。下面列举了它的所有值,允许组合(如[R,L])。 R = Redirect(URL跳转到<replacement-string>地址) NF = Not found(返回404错误给用户,但该文件并未移除,还是保留在网站中) L = Last test if match(如果已经匹配,将不在继续匹配下去) F = Forbidden(跟NF标志相似,) I = Do case-insensitive matching U = Store original url in server Variable HTTP_X_REWRITE_URL(保存原始的url到HTTP_X_REWRITE_URL服务器变量中。) [R] or [R=code]
就像跟我们在ASP.NET使用的Redirect方法一样,重新改变浏览器的方向,跳转到新的指定的URL中。
[R=code]允许我们指定特定的HTTP状态返回码。只能介于301到399。如果超出这个范围。默认会是使用302状态。
RewriteRule ^/goto.aspx?r=(.*)$ $1 [R]
源:http://xxx/goto.aspx?r=http://www.google.com/
目标:http://www.google.com [L]
上面已经简单介绍过。不在说明 [NF]
上面已经简单介绍过。它还可以跟RewriteCond一起配合,来实现自定义的404错误请求。
特别要注意,你所要匹配的文件必须存在,替换的字符串不允许是存在文件名
RewriteRule ^/1008.aspx$ /1.aspx [NF]
1008.aspx文件需要存在,1.aspx不存在,否则无法正常达到我们的结果。
(很奇怪,我不知道是不是我搞错了。但我最终测试的结果确实是这样,文档也没详细说明过,有知道的朋友可以告诉我一下原因) [F]
不在说明。 [I]
模糊匹配 [U]
保存原始的url到HTTP_X_REWRITE_URL服务器变量中。
在ASP.NET你可以用Request.ServerVariables["HTTP_X_REWRITE_URL"]获取原始值。 RewriteCond
RewriteCond <test-string> <pattern> [<modifier flag[,...]>]
类似于条件判断,并且允许多个条件,OR,AND。只有当RewriteCond的Server Variable 匹配所指定的正则表达,RewriteRule才会执行。比如:
RewriteCond %{REMOTE_ADDR} ^(127.0.0.1)$
RewriteRule ^/(.*).aspx$ /$1.aspx
如果我们访问网站的地址的IP来源于127.0.0.1,那么,允许 RewriteRule ^/(.*).aspx$ /$1.aspx
RewriteCond %{REMOTE_ADDR} ^(127.0.0.1)$ [OR]
RewriteCond %{REMOTE_ADDR} ^(192.168.0.10)$
RewriteRule ^/(.*).aspx$ /$1.aspx
添加了OR来多个条件判断
RewriteCond %{REMOTE_ADDR} ^(?!127.0.0.1)([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3})(.*)$
RewriteRule ^/(?!redirected.htm)(.*)$ /redirected.htm Modifier flags有二个值 I=模糊匹配 OR=逻辑判断
从IIRF的RewriteCond的功能上来说,确实很灵活,不知道跟IIS7的重写怎么样,嘻嘻。还没瞧见过呢*^_&。另外。RewriteCond的[Patterns]可以带下面几个参数 -d
Treats the TestString as a pathname and tests if it exists,
and is a directory.
TestString是一个路径名称,并且存在这个路径 -f
Treats the TestString as a pathname and tests if it exists and
is a regular file.
TestString是一个路径名称,并且是一个存在的文件 -s
Treats the TestString as a pathname and tests if it exists and
is a regular file with size greater than zero.
TestString 是一个路径名称,并且存在文件超过0字节
如文档所使用的例子
(1)RewriteCond %{HTTP_URL} (/|\.htm|\.php|\.html|/[^.]*)$ [I] )
(2)RewriteCond %{REQUEST_FILENAME} !-f
(3)RewriteCond %{REQUEST_FILENAME} !-d
(4)RewriteRule ^.*$ /index.aspx [U,L]
(1)如果URL是以htm,php,html(模糊匹配),
(2)URL不是存在文件
(3)URL不是请求的路径
(4)将所有请求跳转到index.aspx,保存原始的URL,之后不在对此进行匹配
在如
RewriteCond %{HTTP_USER_AGENT} ^Mozilla.*
RewriteRule ^/$ /homepage.max.html [L]
IIRF其它配置属性
IterationLimit {integer}从指定的integer后开始匹配RewriteRule组。如果超出RewriteRule个数,默认将会从第8个开始。
MaxMatchCount {integer} RewriteRule组的总个数。
RewriteLog <filename stub> 日志路径
RewriteLogLevel {0,1,2,3,4,5} 日志的等级