Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2283247
  • 博文数量: 276
  • 博客积分: 5998
  • 博客等级: 大校
  • 技术积分: 5175
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-24 14:43
文章分类

全部博文(276)

文章存档

2014年(25)

2013年(11)

2012年(69)

2011年(167)

2010年(4)

分类: LINUX

2011-07-15 07:23:56

原文地址: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、举例说明:

下面是在一个虚拟主机里定义的规则。功能是把client请求的主机前缀不是

和203.81.23.202都跳转到主机前缀为http://.cn,避免当用户在地址栏

写入时不能以会员方式登录网站。

NameVirtualHost 192.168.100.8:80 ServerAdmin webmaster@colorme.com.cn

DocumentRoot "/web/webapp" ServerName .cn

ServerName colorme.com.cn

 

RewriteEngine on #打开rewirte功能

 

#声明Client请求的主机中前缀不是.cn,[NC]的意思是忽略大小写

RewriteCond %{HTTP_HOST} !^.cn [NC]

 

#声明Client请求的主机中前缀不是203.81.23.202,[NC]的意思是忽略大小写

RewriteCond %{HTTP_HOST} !^203.81.23.202 [NC]

 

 #声明Client请求的主机中前缀不为空,[NC]的意思是忽略大小写

RewriteCond %{HTTP_HOST} !^$

 

#含义是如果Client请求的主机中的前缀符合上述条件,则直接进行跳转到

#http://.cn/,[L]意味着立即停止重

#写操作,并不再应用其他重写规则。这里的.*是指匹配所有URL中不包含换行字符,()括号的功

#能是把所有的字符做一个标记,以便于后面的应用.就是引用前面里的(.*)字符。

RewriteRule ^/(.*) http://.cn/ [L]

 

 

例二.将输入 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 ^/(.*) [L]

 

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

1) R[=code](force redirect) 强制外部重定向 强制在替代字符串加上

[: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 中将一个域名转发到另一个域名虚拟主机世界近期更

换了域名,新域名为 更加简短好记。这时需要将原来的域名

webhosting-world.com, 以及论坛所在地址 webhosting-world.com/forums/定向到新的

域名,以便用户可以找到,并且使原来的论坛 URL 继续有效而不出现 404 未找到,比如原

来的 让它在新的域名下继续有

效,点击后转发到 这就需要用 apache

的 Mod_rewrite 功能来实现。在中添加下面的重定向规则:

 

RewriteEngine On #

Redirect webhosting-world.com/forums to bbs.wbhw.com

RewriteCond %{REQUEST_URI} ^/forums/

RewriteRule /forums/(.*) [R=permanent,L] #

Redirect webhosting-world.com to wbhw.com

RewriteCond %{REQUEST_URI} !^/forums/

RewriteRule /(.*) [R=permanent,L]

 

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


        Servername btmuchina.51job.com
        Documentroot /www/htdocs/companyads/campus/btmu
        CustomLog "|/usr/local/sbin/cronolog /var/www/logs/`hostname`_btmuaccess_log%m%d" combined env=!image-request
        RewriteEngine on
        RewriteCond %{REQUEST_URI} ^campus.51job.com/btmu/
        RewriteRule campus.51job.com/btmu/(.*) http://{SERVER_NAME}/$1 [R]


RedirectMatch permanent ^/(.*)$

ServerAlias webhosting-world.com
ServerAdmin admin@webhosting-world.com

ServerName
DocumentRoot /path/to/webhosting-world/root
RewriteEngine On #

Redirect webhosting-world.com/forums to bbs.wbhw.com

RewriteCond %{REQUEST_URI} ^/forums/
RewriteRule /forums/(.*)

[R=permanent,L] #

Redirect webhosting-world.com to wbhw.com

RewriteCond %{REQUEST_URI} !^/forums/
RewriteRule /(.*) [R=permanent,L]

 

URL重定向 例子一:

1.

2. 的功能

RewriteEngine On

RewriteCond %{HTTP_HOST} ^

RewriteCond %{REQUEST_URI} !^user\.php$

RewriteCond %{REQUEST_URI} \.php$

RewriteRule (.*)\.php$ http:///$1/ [R]

RewriteCond %{HTTP_HOST} !^

RewriteRule ^(.+) %{HTTP_HOST} [C]

RewriteRule ^([^\.]+)\.zzz\.com http:///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/

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//sylvan$3?un=$1&%{QUERY_STRING} [L]

 

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

/home/www/ 但是,继续看下去,看到...配置了吗?在这里我就配置了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] 规则)。注意,在

这一句中指明的重写后的地址用的是服务器上的绝对路径,这是内部跳转。如果使用

这样的URL格式,则被称为外部跳转。使用外部跳转的话,浏览着的浏览器中的URL

地址会改变成新的地址,而使用内部跳转则浏览器中的地址不发生改变,看上去更像实际的二

级域名虚拟服务器。这样设置后,重启Apache服务器,测试一下,就大功告成了! 
阅读(1304) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~