Apache的Rewrite规则学习

    1. 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) 传递给下一个处理

      例如:

      RewriteRule ^/abc(.*) /def$1 [PT] # 将会交给/def规则处理

      Alias /def /ghi

      S=num(skip next rule(s)) 跳过num条规则

      E=VAR:VAL(set environment variable) 设置环境变量

    1. 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

  1. 3.实际操作:

    例子: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>

     

    在httpd-vhosts.conf中的写法如下:

    RewriteRule ^/world/([\d]+)_([A-Z]+).html$   /world/index.php?c=world&a=houseDetail&houseId={$1}&housetype={$2} [L]

    RewriteRule ^/pinggu/([a-z]+)/([0-9]+).html /pinggu/index.php?c=pinggu&a=detail&city=$1&newcode=$2&rewrite=1 [NC,NE]

    RewriteRule ^/jiaju/([a-z]+)/jc_c([\d]+).html /jiaju/index.php?c=jiaju&a=buildList&city=$1&cid=$2

    RewriteRule ^/esf_bs/([a-z]*)_?(\d*)_?b?(\d*)/?m?([0-9,]*)x?(\d*)a?([0-9,]*)c?(\d*)p?([0-9,]*)d?(\d*)/$ /esf/index.php?purpose=%B1%F0%CAFB&city=$1&district=$2&comarea=$3&price=$4&orderby=$5&area=$6&tags=$7&buildclass=$8&equipment=$9&rewrite

     

    RewriteRule ^/world/([\d]+)_([A-Z]+).html$ /world/index.php?c=world&a=houseDetail&houseId={$1}&housetype={$2} [L]

     

    #住宅rewrite规则
      RewriteRule^/zf/([a-z]*)_?(\d*)_?(\d*)/?p?(\d*)h?(\d*)m?(\d*)c?(\d*)([a-z0-9]*)/$   $8&&purpose=&city=$1&district=$2&comarea=$3&price=$4&room=$5&rtype=$6&tags=$7  [C,NC]
      RewriteRule^s?(\d*)r?(\d*)o?(\d*)f?(\d*)d?(\d*)p?x?(\d*)&&(.*) /zf/index.php?$7&housetype=$1&railway=$2&towards=$3&floor=$4&equipment=$5&orderby=$6&rewrite [QSA,L,NC]
      
    #别墅rewrite规则
      RewriteRule ^/zf_bs/([a-z]*)_?(\d*)_?(\d*)/?p?(\d*)t?(\d*)m?(\d*)s?(\d*)([a-z0-9]*)/?$ $8&&purpose=%B1%F0%CA%FB&city=$1&district=$2&comarea=$3&price=$4&buildclass=$5&rtype=$6&housetype=$7 [C,NC]
      RewriteRule ^h?(\d*)o?(\d*)f?(\d*)d?(\d*)p?x?(\d*)&&(.*) /zf/index.php?$6&room=$1&towards=$2&floor=$3&equipment=$4&orderby=$5&rewrite [QSA,L,NC]
      
    #写字楼rewrite规则
      RewriteRule ^/zf_app_xzl/([a-z]*)_?(\d*)_?(\d*)/?p?(\d*)a?(\d*)t?(\d*)c?(\d*)r?(\d*)/? /zf/index.php?purpose=%D0%B4%D7%D6%C2%A5&app_type=app&city=$1&district=$2&comarea=$3&price=$4&area=$5&propertysubtype=$6&tags=$7&&railway=$8&rewrite [QSA,L]
      
    #写字楼rewrite规则
     RewriteRule ^/zf_xzl/([a-z]*)_?(\d*)_?(\d*)/?p?(\d*)a?(\d*)t?(\d*)c?(\d*)r?(\d*)/? /zf/index.php?purpose=%D0%B4%D7%D6%C2%A5&city=$1&district=$2&comarea=$3&price=$4&area=$5&propertysubtype=$6&tags=$7&&railway=$8&rewrite [QSA,L]
     
    #写字楼rewrite规则
       RewriteRule ^/zf_xzl/([a-z]*)_?(\d*)_?(\d*)/?p?(\d*)a?(\d*)t?(\d*)c?(\d*)r?(\d*)/? /zf/index.php?purpose=%D0%B4%D7%D6%C2%A5&city=$1&district=$2&comarea=$3&price=$4&area=$5&propertysubtype=$6&tags=$7&&railway=$8&rewrite [QSA,L]   
       RewriteRule ^/zf/([a-z]+)_xm([0-9]+)/$ /zf/index.php?c=zf&a=index&city=$1&projcodes=$2&src=xiaoqu&rewrite [L]

     

     

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。