一共11个参数,红色标注的就是应该匹配的参数.由于apache中最多配置参数为9个,当超过9个的参数时,剩余参数可以当成一个参数进行传递,通过rewite标志C进行关联.
apaceh配置:
RewriteRule ^(.*)/([a-zA-Z]+)/product/list_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_(.*)\.html$ $1/$9.htmls=Home/Goods/lists/tid/$2/cata/$3/catb/$4/catc/$5/catd/$6/cate/$7/catf/$8 [C,NC]
RewriteRule ^(.*)/([0-9]+)_([0-9]+)_([0-9]+)\.html(.*)$ $1/index.php?$5//price/$2/order/$3/p/$4 [QSA,L,NC]
IIS中web.config文件中
<rule name="已导入的规则 8">
<match url="^([a-zA-Z]+)/product/list_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+).html$" ignoreCase="false" />
<action type="Rewrite" url="index.php?s=Home/Goods/lists/tid/{R:1}/cata/{R:2}/catb/{R:3}/catc/{R:4}/catd/{R:5}/cate/{R:6}/catf/{R:7}/price/{R8}/order/{R:9}/p/{R:10}" appendQueryString="true" />
</rule>
Rewrite标志
R[=code](force redirect) 强制外部重定向
G(force URL to be gone) 强制URL为GONE,返回410HTTP状态码。
P(force proxy) 强制使用代理转发。
L(last rule) 表明当前规则是最后一条规则,停止分析以后规则的重写。
N(next round) 重新从第一条规则开始运行重写过程。
C(chained with next rule) 与下一条规则关联
如果规则匹配则正常处理,该标志无效,如果不匹配,那么下面所有关联的规则都跳过
T=MIME-type(force MIME type) 强制MIME类型
NS (used only if no internal sub-request) 只用于不是内部子请求
NC(no case) 不区分大小写
QSA(query string append) 追加请求字符串
NE(no URI escaping of output) 不在输出转义特殊字符
例如:
RewriteRule /foo/(.*) /bar?arg=P1\%3d$1 [R,NE] 将能正确的将/foo/zoo转换成/bar?arg=P1=zed
PT(pass through to next handler) 传递给下一个处理
例如:
RewriteRule ^/abc(.*) /def$1 [PT] # 将会交给/def规则处理
Alias /def /ghi
S=num(skip next rule(s)) 跳过num条规则
E=VAR:VAL(set environment variable) 设置环境变量
RewriteCond标志符
'nocase|NC'(no case)忽略大小
'ornext|OR' (or next condition)逻辑或,可以同时匹配多个RewriteCond条件RewriteRule适用的标志符
'redirect|R [=code]' (force redirect)强迫重写为基于http开头的外部转向(注意URL的变化) 如:[R=301,L]
'forbidden|F' (force URL to be forbidden)重写为禁止访问
'proxy|P' (force proxy)重写为通过代理访问的http路径
'last|L' (last rule)最后的重写规则标志,如果匹配,不再执行以后的规则
'next|N' (next round)循环同一个规则,直到不能满足匹配
'chain|C' (chained with next rule)如果匹配该规则,则继续下面的有Chain标志的规则。
'type|T=MIME-type' (force MIME type)指定MIME类型
'nosubreq|NS' (used only if no internal sub-request)如果是内部子请求则跳过
'nocase|NC' (no case)忽略大小
'qsappend|QSA' (query string append)附加查询字符串
'noescape|NE' (no URI escaping of output)禁止URL中的字符自动转义成%[0-9]+的形式。
'passthrough|PT' (pass through to next handler)将重写结果运用于mod_alias
'skip|S=num' (skip next rule(s))跳过下面几个规则
'env|E=VAR:VAL' (set environment variable)添加环境变量
Rewrite时服务器变量:
HTTP headers:HTTP_USER_AGENT, HTTP_REFERER, HTTP_COOKIE, HTTP_HOST, HTTP_ACCEPT
connection & request: REMOTE_ADDR, QUERY_STRING
server internals: DOCUMENT_ROOT, SERVER_PORT, SERVER_PROTOCOL
system stuff: TIME_YEAR, TIME_MON, TIME_DAY
Rewrite规则表达式的说明:
. 匹配任何单字符
[chars] 匹配字符串:chars
[^chars] 不匹配字符串:chars
text1|text2 可选择的字符串:text1或text2
? 匹配0到1个字符
* 匹配0到多个字符
+ 匹配1到多个字符
^ 字符串开始标志
$ 字符串结束标志
\n 转义符标志
反向引用 $N 用于 RewriteRule 中匹配的变量调用(0 <= N <= 9)
反向引用 %N 用于 RewriteCond 中最后一个匹配的变量调用(1 <= N <= 9)
实际操作:例子:RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ^MSIE [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Opera [NC]
RewriteRule ^.* - [F,L] 这里”-”表示没有替换,浏览器为IE和Opera的访客将被禁止访问。
例子:
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ([^/]+)$ /test/$1.php
#for example: /test/admin => /test/admin.php
RewriteRule ([^/]+)\.html$ /test/$1.php [L]
#for example: /test/admin.html => /test/admin.php
限制目录只能显示图片
< IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !^.*\.(gif|jpg|jpeg|png|swf)$
RewriteRule .*$ - [F,L]
< /IfModule>
例子2:.htaccess文件
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
RewriteRule ^(.*)/login\.html$ $1/index.php?s=Home/Public/login [L]
RewriteRule ^(.*)/gsjj\.html$ $1/index.php?s=Home/Public/gsjj [L]
RewriteRule ^(.*)/lxfs\.html$ $1/index.php?s=Home/Public/lxfs [L]
RewriteRule ^(.*)/logout\.html$ $1/index.php?s=Home/Public/logout [L]
RewriteRule ^(.*)/forgotpwd\.html$ $1/index.php?s=Home/Public/forgotpwd [L]
RewriteRule ^(.*)/register\.html$ $1/index.php?s=Home/Public/register [L]
RewriteRule ^(.*)/categoryall$ $1/index.php?s=Home/categoryall/index [QSA]
RewriteRule ^(.*)/categoryall/$ $1/index.php?s=Home/categoryall/index [QSA]
RewriteRule ^(.*)/([a-zA-Z]+)/category$ $1/index.php?s=Home/category/index/tid/$2 [QSA]
RewriteRule ^(.*)/([a-zA-Z]+)/category/$ $1/index.php?s=Home/category/index/tid/$2 [QSA]
RewriteRule ^(.*)/product$ $1/index.php?s=Home/Goods/search/ [QSA]
RewriteRule ^(.*)/product/$ $1/index.php?s=Home/Goods/search/ [QSA]
RewriteRule ^(.*)/([a-zA-Z]+)/product$ $1/index.php?s=Home/Goods/index/tid/$2 [QSA]
RewriteRule ^(.*)/([a-zA-Z]+)/product/$ $1/index.php?s=Home/Goods/index/tid/$2 [QSA]
RewriteRule ^(.*)/([a-zA-Z]+)/product/list_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_(.*)\.html$ $1/$9.htmls=Home/Goods/lists/tid/$2/cata/$3/catb/$4/catc/$5/catd/$6/cate/$7/catf/$8 [C,NC]
RewriteRule ^(.*)/([0-9]+)_([0-9]+)_([0-9]+)\.html(.*)$ $1/index.php?$5//price/$2/order/$3/p/$4 [QSA,L,NC]
RewriteRule ^(.*)/product/list1_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_(.*)\.html$ $1/$9.htmls=Home/Goods/list1s/cata/$2/catb/$3/catc/$4/catd/$5/cate/$6/catf/$7/price/$8 [C,NC]
RewriteRule ^(.*)/([0-9]+)_([0-9]+)\.html(.*)$ $1/index.php?$4//order/$2/p/$3 [QSA,L,NC]
RewriteRule ^(.*)/([a-zA-Z]+)/product/p([0-9]+)\.html$ $1/index.php?s=Home/Goods/detail/tid/$2/id/$3 [L]
RewriteRule ^(.*)/comment/$ $1/index.php?s=Home/Comments/index [QSA]
RewriteRule ^(.*)/comment/list_([0-9]+)_([0-9]+)\.html$ $1/index.php?s=Home/Comments/lists/cid/$2/p/$3 [L]
RewriteRule ^(.*)/ask$ $1/index.php?s=Home/Ask/index [QSA]
RewriteRule ^(.*)/ask/$ $1/index.php?s=Home/Ask/index [QSA]
RewriteRule ^(.*)/ask/list_([0-9]+)_([0-9]+)_([0-9]+)\.html$ $1/index.php?s=Home/Ask/lists/cid/$2/tid/$3/p/$4 [L]
RewriteRule ^(.*)/ask/p([0-9]+)\.html$ $1/index.php?s=Home/Ask/detail/id/$2 [L]
RewriteRule ^(.*)/news/$ $1/index.php?s=Home/News/index [QSA]
RewriteRule ^(.*)/news/([0-9]+)\.html$ $1/index.php?s=Home/News/detail/id/$2 [L]
RewriteRule ^(.*)/mxcp$ $1/index.php?s=Home/Star/index [QSA]
RewriteRule ^(.*)/mxcp/$ $1/index.php?s=Home/Star/index [QSA]
RewriteRule ^(.*)/([a-zA-Z]+)/mxcp/([0-9]+)\.html$ $1/index.php?s=Home/Star/detail/tid/$2/id/$3 [L]
RewriteRule ^(.*)/xgzt$ $1/index.php?s=Home/Special/index [QSA]
RewriteRule ^(.*)/xgzt/$ $1/index.php?s=Home/Special/index [QSA]
RewriteRule ^(.*)/xgzt/([0-9]+)\.html$ $1/index.php?s=Home/Special/detail/id/$2 [L]
RewriteRule ^(.*)/gallery$ $1/index.php?s=Home/Pics/index [QSA]
RewriteRule ^(.*)/gallery/$ $1/index.php?s=Home/Pics/index [QSA]
RewriteRule ^(.*)/gallery/list_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)\.html$ $1/index.php?s=Home/Pics/lists/cata/$2/catb/$3/catc/$4/catd/$5/cate/$6/catf/$7/cid/$8/p/$9 [QSA]
RewriteRule ^(.*)/gallery/g([0-9]+)\.html$ $1/index.php?s=Home/Pics/detail/id/$2 [L]
RewriteRule ^(.*)/gallery/p([0-9]+)\.html$ $1/index.php?s=Home/Pics/productindex/id/$2 [L]
RewriteRule ^(.*)/ppjs\.html$ $1/index.php?s=Home/Index/brandintroduction [L]
RewriteRule ^(.*)/rdjj$ $1/index.php?s=Home/BrandBaike/index [QSA]
RewriteRule ^(.*)/rdjj/$ $1/index.php?s=Home/BrandBaike/index [QSA]
RewriteRule ^(.*)/rdjj/([0-9]+)\.html$ $1/index.php?s=Home/BrandBaike/detail/id/$2 [L]
RewriteRule ^(.*)/([a-zA-Z]+)/$ $1/index.php?s=Home/Channel/index/id/$2 [QSA]
RewriteRule ^(.*)/shopcart$ $1/index.php?s=Home/Shopcart/index [L]
RewriteRule ^(.*)/shopcart/$ $1/index.php?s=Home/Shopcart/index [L]
</IfModule>