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、举例说明:

下面是在一个虚拟主机里定义的规则。功能是把client请求的主机前缀不是www.colorme.com和203.81.23.202都跳转到主机前缀为http://www.colorme.com.cn,避免当用户在地址栏写入http://colorme.com.cn时不能以会员方式登录网站。

NameVirtualHost 192.168.100.8:80

ServerAdmin webmaster@colorme.com.cn
DocumentRoot "/web/webapp"
ServerName www.colorme.com.cn
ServerName colorme.com.cn
RewriteEngine on #打开rewirte功能
RewriteCond %{HTTP_HOST} !^www.colorme.com.cn [NC] #声明Client请求的主机中前缀不是www.colorme.com.cn,[NC]的意思是忽略大小写
RewriteCond %{HTTP_HOST} !^203.81.23.202 [NC] #声明Client请求的主机中前缀不是203.81.23.202,[NC]的意思是忽略大小写
RewriteCond %{HTTP_HOST} !^$ #声明Client请求的主机中前缀不为空,[NC]的意思是忽略大小写
RewriteRule ^/(.*) http://www.colorme.com.cn/ [L]
#含义是如果Client请求的主机中的前缀符合上述条件,则直接进行跳转到http://www.colorme.com.cn/,[L]意味着立即停止重写操作,并不再应用其他重写规则。这里的.*是指匹配所有URL中不包含换行字符,()括号的功能是把所有的字符做一个标记,以便于后面的应用.就是引用前面里的(.*)字符。

例二.将输入 folio.test.com 的域名时跳转到profile.test.com

listen 8080
NameVirtualHost 10.122.89.106:8080
ServerAdmin webmaster@colorme.com.cn
DocumentRoot "/usr/local/www/apache22/data1/"
ServerName profile.test.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^folio.test.com [NC]
RewriteRule ^/(.*) http://profile.test.com/ [L]

3.Apache mod_rewrite规则重写的标志一览

1) R[=code](force redirect) 强制外部重定向
强制在替代字符串加上http://thishost[:thisport]/前缀重定向到外部的URL.如果code不指定,将用缺省的302 HTTP状态码。
2) F(force URL to be forbidden)禁用URL,返回403HTTP状态码。
3) G(force URL to be gone) 强制URL为GONE,返回410HTTP状态码。
4) P(force proxy) 强制使用代理转发。
5) L(last rule) 表明当前规则是最后一条规则,停止分析以后规则的重写。
6) N(next round) 重新从第一条规则开始运行重写过程。
7) C(chained with next rule) 与下一条规则关联

如果规则匹配则正常处理,该标志无效,如果不匹配,那么下面所有关联的规则都跳过。

8) T=MIME-type(force MIME type) 强制MIME类型
9) NS (used only if no internal sub-request) 只用于不是内部子请求
10) NC(no case) 不区分大小写
11) QSA(query string append) 追加请求字符串
12) NE(no URI escaping of output) 不在输出转义特殊字符
例如:RewriteRule /foo/(.*) /bar?arg=P1\%3d$1 [R,NE] 将能正确的将/foo/zoo转换成/bar?arg=P1=zed
13) PT(pass through to next handler) 传递给下一个处理
例如:
   RewriteRule ^/abc(.*) /def$1 [PT] # 将会交给/def规则处理
   Alias /def /ghi
14) S=num(skip next rule(s)) 跳过num条规则
15) E=VAR:VAL(set environment variable) 设置环境变量

4.Apache rewrite例子集合

在 httpd 中将一个域名转发到另一个域名虚拟主机世界近期更换了域名,新域名为 www.wbhw.com, 更加简短好记。这时需要将原来的域名webhosting-world.com, 以及论坛所在地址 webhosting-world.com/forums/定向到新的域名,以便用户可以找到,并且使原来的论坛 URL 继续有效而不出现 404 未找到,比如原来的http://www.webhosting-world.com/forums/-f60.html, 让它在新的域名下继续有效,点击后转发到http://bbs.wbhw.com/-f60.html, 这就需要用 apache 的 Mod_rewrite 功能来实现。

在中添加下面的重定向规则:

RewriteEngine On
# Redirect webhosting-world.com/forums to bbs.wbhw.com
RewriteCond %{REQUEST_URI} ^/forums/
RewriteRule /forums/(.*) http://bbs.wbhw.com/$1 [R=permanent,L]
# Redirect webhosting-world.com to wbhw.com
RewriteCond %{REQUEST_URI} !^/forums/
RewriteRule /(.*) http://www.wbhw.com/$1 [R=permanent,L]

添加了上面的规则以后, 里的全部内容如下:

ServerAlias webhosting-world.com
ServerAdmin admin@webhosting-world.com
DocumentRoot /path/to/webhosting-world/root
ServerName www.webhosting-world.com
RewriteEngine On
# Redirect webhosting-world.com/forums to bbs.wbhw.com
RewriteCond %{REQUEST_URI} ^/forums/
RewriteRule /forums/(.*) http://bbs.wbhw.com/$1 [R=permanent,L]
# Redirect webhosting-world.com to wbhw.com
RewriteCond %{REQUEST_URI} !^/forums/
RewriteRule /(.*) http://www.wbhw.com/$1 [R=permanent,L]

URL重定向

例子一:

1.http://www.zzz.com/xxx.php-> http://www.zzz.com/xxx/
2.http://yyy.zzz.com-> http://www.zzz.com/user.php?username=yyy 的功能
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.zzz.com
RewriteCond %{REQUEST_URI} !^user\.php$
RewriteCond %{REQUEST_URI} \.php$
RewriteRule (.*)\.php$ http://www.zzz.com/$1/ [R]
RewriteCond %{HTTP_HOST} !^www.zzz.com
RewriteRule ^(.+) %{HTTP_HOST} [C]
RewriteRule ^([^\.]+)\.zzz\.com http://www.zzz.com/user.php?username=$1

例子二:

/type.php?typeid=*   --> /type*.html
/type.php?typeid=*&page=*   --> /type*page*.html
RewriteRule ^/type([0-9]+).html$ /type.php?typeid=$1 [PT]
RewriteRule ^/type([0-9]+)page([0-9]+).html$ /type.php?typeid=$1&page=$2 [PT]

5.使用Apache的URL Rewrite配置多用户虚拟服务器

要实现这个功能,首先要在DNS服务器上打开域名的泛域名解析(自己做或者找域名服务商做)。比如,我就把 *.semcase.com和 *.semcase.cn全部解析到了我的这台Linux Server上。

然后,看一下我的Apache中关于*.semcase.com的虚拟主机的设定。

#*.com,*.osall.net

ServerAdmin webmaster@semcase.com
DocumentRoot /home/www/www.semcase.com
ServerName dns.semcase.com
ServerAlias dns.semcase.com semcase.com semcase.net *.semcase.com *.semcase.net
CustomLog /var/log/httpd/osa/access_log.log" common
    ErrorLog /var/log/httpd/osa/error_log.log"
AllowOverride None
Order deny,allow
#AddDefaultCharset GB2312   
  
     
RewriteEngine on      
RewriteCond %{HTTP_HOST}        ^[^.]+\.osall\.(com|net)$      
RewriteRule ^(.+)     %{HTTP_HOST}$1   [C]      
RewriteRule ^([^.]+)\.osall\.(com|net)(.*)$
/home/www/www.semcase.com/sylvan$3?un=$1&%{QUERY_STRING} [L]

在这段设定中,我把*.semcase.net和*.semcase.com 的Document Root都设定到了 /home/www/www.semcase.com

但是,继续看下去,看到...配置了吗?在这里我就配置了URL Rewrite规则。
RewriteEngine on #打开URL Rewrite功能
RewriteCond %{HTTP_HOST} ^[^.]+.osall.(com|net)$ #匹配条件,如果用户输入的URL中主机名是类似 xxxx.semcase.com 或者 xxxx.semcase.cn 就执行下面一句
RewriteRule ^(.+) %{HTTP_HOST}$1 [C] #把用户输入完整的地址(GET方式的参数除外)作为参数传给下一个规则,[C]是Chain串联下一个规则的意思
RewriteRule ^([^.]+).osall.(com|net)(.*)$ /home/www/dev.semcase.com/sylvan$3?un=$1&%{QUERY_STRING} [L]
# 最关键的是这一句,使用证则表达式解析用户输入的URL地址,把主机名中的用户名信息作为名为un的参数传给/home/www/dev.semcase.com目录下的脚本,并在后面跟上用户输入的GET方式的传入参数。并指明这是最后一条规则([L]规则)。注意,在这一句中指明的重写后的地址用的是服务器上的绝对路径,这是内部跳转。如果使用http://xxxx这样的URL格式,则被称为外部跳转。使用外部跳转的话,浏览着的浏览器中的URL地址会改变成新的地址,而使用内部跳转则浏览器中的地址不发生改变,看上去更像实际的二级域名虚拟服务器。

例子:

<FilesMatch "\.(bak|inc|lib|sh|tpl|lbi|dwt)$">
    order deny,allow
    deny from all
</FilesMatch>

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(flowerworld.cn)(:80)? [NC]
RewriteRule ^(.*) http://www.flowerworld.com.cn/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.flowerworld.cn)(:80)? [NC]
RewriteRule ^(.*) http://www.flowerworld.com.cn/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(flowerworld.com.cn)(:80)? [NC]
RewriteRule ^(.*) http://www.flowerworld.com.cn/$1 [R=301,L]

RewriteRule ^index\.html$    index\.php [QSA,L]
RewriteRule ^m\.html$   view/admin/adminView\.php?pageAction=quit [QSA,L]

RewriteRule ^info/([0-9]+)\.html$ view/infoView.php?pageAction=viewInfo&id=$1 [QSA,L]
RewriteRule ^sell/([0-9]+)\.html$ view/module/sellView.php?pageAction=viewSellInfo&sellId=$1 [QSA,L]

RewriteRule ^superMarket/([0-9]+)\.html$ view/module/superMarketView.php?pageAction=viewSuperMarketInfo&superMarketId=$1 [QSA,L]

RewriteRule ^product/([0-9]+)\.html$ view/productPostView.php?pageAction=productPostShow&id=$1 [QSA,L]

RewriteRule ^productClass/([0-9]+)\.html$ view/productView.php?pageAction=productClassIndex&id=$1 [QSA,L]

RewriteRule ^enterprise/([0-9]+)/(.*)-([0-9]+)-([0-9]+)$ view/enterpriseMemberView.php?pageAction=memberShow&id=$1&actionShow=$2&showType=$3&appendId=$4 [QSA,L]
RewriteRule ^enterprise/([0-9]+)/(.*)$ view/enterpriseMemberView.php?pageAction=memberShow&id=$1&actionShow=$2 [QSA,L]
RewriteRule ^enterprise/([0-9]+)\.html$ view/enterpriseMemberView.php?pageAction=memberShow&id=$1 [QSA,L]
RewriteRule ^enterprise/([0-9]+)$ view/enterpriseMemberView.php?pageAction=memberShow&id=$1 [QSA,L]

RewriteRule ^news\.html$ view/newsView.php [QSA,L]

RewriteRule ^enterprise\.html$ view/enterpriseView.php [QSA,L]
RewriteRule ^quotedPrice\.html$ newView/searchQpView.php [QSA,L]
RewriteRule ^product\.html$ view/productView.php [QSA,L]
RewriteRule ^sell\.html$ view/module/sellView.php [QSA,L]
RewriteRule ^wantToBuy\.html$ view/module/wantToBuyView.php [QSA,L]
RewriteRule ^superMarket\.html$ view/module/superMarketView.php [QSA,L]
RewriteRule ^help\.html$ view/newsView.php?pageAction=help [QSA,L]

RewriteRule ^column/([0-9]+)\.html$ view/newsView.php?pageAction=column&id=$1 [QSA,L]

RewriteRule ^product/([0-9]+)\.html$ view/productPostView.php?pageAction=productPostShow&id=$1 [QSA,L]

RewriteRule ^enterprise/([0-9]+)/(.*)-([0-9]+)-([0-9]+).html$ view/enterpriseMemberView.php?pageAction=memberShow&id=$1&actionShow=$2&showType=$3&appendId=$4 [QSA,L]

//结果是 enterprise/32/open-12-13.html
//32是参数1,open是参数2,12是参数3,13是参数4

转载于:https://www.cnblogs.com/greatwang/archive/2011/11/29/2648242.html

【引用】.htaccess RewriteRule详解相关推荐

  1. PHP的传值与引用,php 传值与传引用的区别详解

    摘要 腾兴网为您分享:php 传值与传引用的区别详解,掌上公交,信用管家,天翼阅读,平安知鸟等软件知识,以及k歌达人,ml2010打印机驱动,维也纳大学app,建玛特,网盘快搜,中国禁毒数字展览馆,爱 ...

  2. java 引用传递_详解java的值传递、地址传递、引用传递

    详解java的值传递.地址传递.引用传递 一直来觉得对值传递和地址传递了解的很清楚,刚才在开源中国上看到一篇帖子介绍了java中的值传递和地址传递,看完后感受颇深.下边总结下以便更容易理解. 按照以前 ...

  3. Unity文件、文件引用、Meta详解

    原文链接:https://blog.uwa4d.com/archives/USparkle_inf_UnityEngine.html 这是侑虎科技第381篇原创文章,感谢作者陈广忠供稿.欢迎转发分享, ...

  4. .htaccess文件详解

    好记性不如烂笔头,这个东西来回记了两次,完了用的时候还是问百度,今天决定记载下来.虽然文章类型是转载,可是这都是我自己敲的,我自己总结的当然了,我只是借鉴了别人的知识成果. .htaccess文件是a ...

  5. 伪静态与重定向--RewriteRule详解

    伪静态与重定向--RewriteRule 环境:windows 10,phpstudy,sublime text.服务器使用Apache,网站根目录为E:\phpstudy\www\,所以.htacc ...

  6. Apache伪静态(Rewrite).htaccess文件详解以及RewriteCond 规则

    Htaccess(超文本访问)是一个简单的配置文件,它允许设计师,开发者和程序员通过它来改变Apache Web服务器的配置. 这些功能包括用户重定向.URL重写(url rewrite,国内很多称为 ...

  7. 一个普通handler会持有activity引用吗_详解handler机制

    Android中有很多机制,打开源码最先遇到的应该就是handler机制了,handler主要为了解决线程间通讯的问题,首先看一下handler该怎么用. ##handler的用法 1.在主线程中用h ...

  8. python自己创建模块引用失败_详解Python import方法引入模块的实例 Python怎么import自己写的模块...

    python中 import导入模块失败的问题? python中的import引用不了模块我傻,为你傻;我痛,为你痛;深夜里,你是我一种惯性的回忆. 为什么我用from lianxi import*就 ...

  9. 引用java8里的方法_Java8中方法引用的使用详解

    1. 引言 Java8中最受广大开发中喜欢的变化之一是因为引入了 lambda 表达式,因为这些表达式允许我们放弃匿名类,从而大大减少了样板代码,并提高了可读性. 方法引用是lambda表达式的一种特 ...

最新文章

  1. 再见了,收费的Navicat。
  2. @Data注解使用后get set报错解决方法
  3. 计算机硬件的组装实践,毕业论文-计算机硬件组装实践.doc
  4. 收藏 | 2015年度大数据应用经典案例Top100
  5. mysql分库分表风险_数据库分库分表存在的问题及解决方案
  6. sublime配置python3_Sublime Text 3 Python3环境配置
  7. [Everyday Mathematics]20150203
  8. mysql快速随机_MySQL随机取数据最高效的方法
  9. java local_java.time.LocalDateTime with()方法
  10. python excel 添加数据_使用pyexcel python在电子表格中添加行数据
  11. elasticsearch报错exceptions.RequestError(400, u'mapper_parsing_exception', u'No handler field..
  12. Forget Yourself
  13. 在克隆环境上分离httpd和subversion。
  14. ORB feature to FAST,定向快速旋转简报
  15. 二分查找算法java实现
  16. 1型错误和2型错误_沈阳地铁1号线太原街站导向牌出现错误!
  17. 基于STM32的物联网健康监测系统设计(附源码)
  18. 【射影几何02】拓广平面
  19. wps免费下载 wps文档怎么转成加密的PDF文档呢?
  20. 抖音V1.7.9调研报告

热门文章

  1. SQL时间操作常用语句
  2. CVPR 2019 | CVPR2019 论文接收列表
  3. python 调用bat失败_pyinstaller打包的exe太大?你需要嵌入式python玄学 惊喜篇
  4. js取整数、取余数的方法总结
  5. 微信小程序 媒体组件
  6. 考研和谈恋爱冲突吗?如何处理呢?
  7. 连接数据库实现图书管理系统
  8. JavaScript解析JSON格式方法
  9. python显示竖着的文字_Python+OpenCV竖版古籍文字分割
  10. Android源码博文集锦2