一.Apache的rewrite
1、Rewrite规则简介:
Rewirte主要的功能就是实现URL的跳转,它的正则表达式是基于Perl语言。可基于服务器级的
(httpd.conf)和目录级的(.htaccess) 两种方式。如果要想用到rewrite模块,必须先安装或加
载rewrite模块。方法有两种一种是编译apache的时候就直接安装rewrite模块,另一种是编译
apache时以DSO模式安装apache,然后再利用源码和apxs来安装rewrite模块。基于服务器级
的(httpd.conf)有两种方法,一种是在httpd.conf的全局下直接利用RewriteEngine on来打
开rewrite功能;另一种是在局部里利用RewriteEngine on来打开rewrite功能,下面将会举例说
明,需要注意的是,必须在每个virtualhost里用RewriteEngine on来打开rewrite功能。否则
virtualhost里没有RewriteEngine on它里面的规则也不会生效。
基于目录级的(.htaccess),要注意一点那就是必须打开此目录的FollowSymLinks属性且在
.htaccess里要声明RewriteEngine on。2.字符含义介绍:
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) 传递给下一个处理
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
3.实际操作
在httpd-vhosts.conf中的写法如下:
RewriteRule ^/jiaju/([a-z]+)/des.html /jiaju/index.php?c=jiaju&a=designerList&city=$1
RewriteRule ^/jiaju/([a-z]+)/des_r([\d]+).html /jiaju/index.php?c=jiaju&a=designerList&city=$1&DesignPrice=$2
RewriteRule ^/jiaju/([a-z]+)/des_c([\d]+).html /jiaju/index.php?c=jiaju&a=designerList&city=$1&DesignStyle=$2
RewriteRule ^/jiaju/([a-z]+)/des_s([\d]+).html /jiaju/index.php?c=jiaju&a=designerList&city=$1&sortResult=$2
RewriteRule ^/jiaju/([a-z]+)/des_r([\d]+)_c([\d]+).html
/jiaju/index.php?c=jiaju&a=designerList&city=$1&DesignPrice=$2&DesignStyle=$3
RewriteRule ^/jiaju/([a-z]+)/des_s([\d]+)_r([\d]+).html
/jiaju/index.php?c=jiaju&a=designerList&city=$1&sortResult=$2&DesignPrice=$3
RewriteRule ^/jiaju/([a-z]+)/des_s([\d]+)_c([\d]+).html
/jiaju/index.php?c=jiaju&a=designerList&city=$1&sortResult=$2&DesignStyle=$3
RewriteRule ^/jiaju/([a-z]+)/des_s([\d]+)_r([\d]+)_c([\d]+).html
/jiaju/index.php?c=jiaju&a=designerList&city=$1&sortResult=$2&DesignPrice=$3&DesignStyle=$4
以上rewrite可用一条规则匹配,如下
RewriteRule ^/jiaju/([a-z]+)/des(?:_s([\d]*))?(?:_r([\d]))?(?:_c([\d]*))?.html
/jiaju/index.php?c=jiaju&a=designerList&city=$1&sortResult=$2&DesignPrice=$3&DesignStyle=$4
#装修报价rewrite规则
RewriteRule ^/jiaju/([a-z]+)/zxbj(?:_r([\d]*))?(?:_s([\d]*))?(?:_f([\d]*))?(?:_a([\d]*))?(?:_p([\d]*))?.html
/jiaju/index.php?c=jiaju&a=zxbj&city=$1&CaseRoom=$2&CaseStyle=$3&CasePictureType=$4&Area=$5&Price=$6上面一条亦可拆分成以下两条规则:
RewriteRule ^/jiaju/([a-z]+)/zxbj(?:_r([\d]*))?(?:_s([\d]*))?(.*) $4city=$1&CaseRoom=$2&CaseStyle=$3 [C,NC]
RewriteRule ^(?:_f([\d]*))?(?:_a([\d]*))?(?:_p([\d]*))?.html(.*)
/jiaju/index.php?c=jiaju&a=zxbj&$4&CasePictureType=$1&Area=$2&Price=$3 [QSA,NC,L]可以从以上规则中感受apache的rewrite规则。
值得注意的是apache的rewrite规则参数若超过十个则得分成两条来写! 而在nginx中是通过重命名的方式实现,具体讲解见下面。
二.Nginx的rewrite规则:
Nginx的rewrite重定向相当于apache的RewriteRule,具体讲解如下:
它可以用在server,location 和IF条件判断块中,例子如下
server
{
#指定监听端口
listen 80;
#指定服务器域名
server_name mm.test.fang.com;
#指定能解析的页面
index index.html index.htm index.php;
#指定实际的工作代码路径
root /opt/lampp/htdocs/www;
rewrite ^/ http://www.c1gstudio.com/;
access_log off;
}
location ~* ^/zf_bs/ {
#rewrite ^/zf_bs/(.*) /zf_bs/$1;
rewrite ^/([a-z]*)_?(\d*)_?(\d*)/?p?([0-9,]*)t?(\d*)m?(\d*)s?(\d*)h?(\d*)o?(\d*)f?(?P<f>\d*)d?(?P<d>\d*)p?x?(?P<px>\d*)/? /zf/?purpose=%B1%F0%CA%FB&city=$1&district=$2&comarea=$3&price=$4&buildclass=$5&rtype=$6&housetype=$7&room=$8&towards=$9&floor=$f&equipment=$d&orderby=$px&rewrite last;
}这一条rewrite规则在apache中的实现:
RewriteRule
^/([a-z]*)_?(\d*)_?(\d*)/?p?([0-9,]*)t?(\d*)m?(\d*)s?(\d*)h?(\d*)(.*)/$ $9&&purpose=%B1%F0%CA%FB&city=$1&district=$2&comarea=$3&price=$4&buildclass=$5&rtype=$6&housetype=$7&room=$8 [C,NC]
RewriteRule
^o?(\d*)f?(\d*)d?(\d*)x?(\d*)&&(.*) /zf/index.php?$5&towards=$1&floor=$2&equipment=$3&orderby=$4&rewrite [QSA,L,NC]
nginx中若多于十个条件的用重命名的方式表示 ,如f?(?P<f>\d*) (实际上是一种匹配规则),但在nginx中不支持此法,只能分成两条来写
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /nginx-ie/$1 break;
}
关键字符介绍:
last – 此条规则匹配完后不在向下匹配 ,相当于apache的L;
break– 中止Rewirte,不在继续匹配;
redirect – 返回临时重定向的HTTP状态302;
permanent – 返回永久重定向的HTTP状态301;匹配判断的特殊字符:
~ 为区分大小写匹配;!~为区分大小写不匹配;
~*为不区分大小写匹配;
!~为不区分大小写不匹配。
(nginx更多介绍将“nginx学习篇”)