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]

时间: 2025-01-19 22:26:51

Apache的Rewrite规则学习的相关文章

单独编译apache的rewrite模块

 单独编译apache的rewrite模块 当我们编译安装好apache的时候,就像这样: ./configure \ --prefix=/usr/local/apache \   //安装的路径 --with-included-apr \        // 跨平台 --enable-so \                 //启用DSO --enable-deflate=shared \    //动态共享的方式编译deflate模块 --enable-expires=shared \  

apache的rewrite机制

当我们使用thinkphp的时候,比如说我们访问一个Test控制器的test方法,http://localhost/index.php/Test/test/1.html,那个这个1是用get方式传递的参数,假如这个参数为id,那么我们其实访问的真实地址是http://localhost/index.php/Test/test?id=1.html,用过thinkphp的都知道,其实这个就是伪静态,通过配置tp配置文件的url模式可以实现,但是伪静态的实现,其实也可以用apache的rewrite机

Apache禁止解析php 禁止指定user_agent Apache通过rewrite限制某个目录

<Directory /data/www/data> php_admin_flag engine off <filesmatch "(.*)php"> Order deny,allow Deny from all </filesmatch> </Directory> 禁止指定user_agent <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_USER_

页面静态化3 --- 伪静态技术之Apache的rewrite机制

  Apache的rewrite机制: 意思就是,你发送的地址,比如:http://localhost/news-id67.html会被Apache改写成http://localhost/news.php?id=67; Apache本身内置了一个模块:(httpd.conf) #LoadModule rewrite_module modules/mod_rewrite.so ---- 去掉注释,开启Apache内置的重写模块开启模块过后,这个模块把news-id67.html重写为news.ph

apache配置rewrite及.htaccess文件(转载)

今天看到一个哥们的帖子发了个rewrite的帖子,以前也写过一个,配置挺简单的,但当时没注意这个问题,当时没有用到.htaccess文件,在机子上测试了一下,发现确实没法用,于是开始找问题的所在. 自己从头搭建了一下: 1.  首先确定您使用的 Apache 版本,及是否加载了 mod_Rewrite 模块. 简单方法 echo phpinfo();  看apache的mod里有没有rewrite 如果有跳过此步 Apache 1.x 的用户请检查 conf/httpd.conf 中是否存在如下

基于ZF中的.htaccess文件学习Apache的Rewrite语法

首先我们看一下这个.htaccess文件: RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L] 这里面涉及到了3个指令: 1.RewriteEngine  最简单,控制

使用Apache的rewrite技术

使用Apache的rewrite技术 做PHP项目中须要用到URL重定向技术,基本上的需求就是把比方 /user/heiyeluren 重定向到 /user.php?uid=heiyeluren 之类的URL上,当然,你也可以把 /article/200707291011.html重定向到 /article.php?id=200507291011 之类的,模拟好像是静态页面,可以隐藏URL真实地址,有助于雨鞋主要的安全防范等等.那么好像rewrite是个非常好的解决的方法. 要在Apache里执

【转】CentOs中Apache开启rewrite模块详解

rewrite是apache环境的一个伪静态功能了,如果我们没有没让Apache开启rewrite功能,网站上所有的rewrite规则都不可使用. centos的配置文件放在: /etc/httpd/conf/httpd.conf 打开文件找到: LoadModule rewrite_module modules/mod_rewrite.so 将前面"#"去掉,如果不存在则添加上句. 如果你的网站是根目录的话:找到 <Directory /> Options FollowS

如何在Ubuntu下启动Apache的Rewrite功能

在终端中执行 sudo a2enmod rewrite 指令后,即启用了 Mod_rewrite 模块. 另外,也可以通过将 /etc/apache2/mods-available/rewrite.load 连接到 /etc/apache2/mods-enabled/rewrite.load 来打开 Mod_rewrite 模块.用指令操作就是: sudo ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabl