Apache Rewrite 规则详解知识大全

Rewrite是一种服务器的重写脉冲技术,它可以使得服务器可以支持 URL 重写,是一种最新流行的服务器技术。它还可以实现限制特定IP访问网站的功能。

1、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) 设置环境变量

2、RewriteCond标志符

\'nocase|NC\'(no case)忽略大小

\'ornext|OR\' (or next condition)逻辑或,可以同时匹配多个RewriteCond条件RewriteRule适用的标志符

\'redirect|R [=code]\' (force redirect)强迫重写为基于http开头的外部转向(注意URL的变化) 如:[R=301,L]

\'forbidden|F\' (force URL to be forbidden)重写为禁止访问

\'proxy|P\' (force proxy)重写为通过代理访问的http路径

\'last|L\' (last rule)最后的重写规则标志,如果匹配,不再执行以后的规则

\'next|N\' (next round)循环同一个规则,直到不能满足匹配

\'chain|C\' (chained with next rule)如果匹配该规则,则继续下面的有Chain标志的规则。

\'type|T=MIME-type\' (force MIME type)指定MIME类型

\'nosubreq|NS\' (used only if no internal sub-request)如果是内部子请求则跳过

\'nocase|NC\' (no case)忽略大小

\'qsappend|QSA\' (query string append)附加查询字符串

\'noescape|NE\' (no URI escaping of output)禁止URL中的字符自动转义成%[0-9]+的形式。

\'passthrough|PT\' (pass through to next handler)将重写结果运用于mod_alias

\'skip|S=num\' (skip next rule(s))跳过下面几个规则

\'env|E=VAR:VAL\' (set environment variable)添加环境变量

3、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

4、Rewrite规则表达式的说明:

. 匹配任何单字符

[chars] 匹配字符串:chars

[^chars] 不匹配字符串:chars

text1|text2 可选择的字符串:text1或text2

? 匹配0到1个字符

* 匹配0到多个字符

+ 匹配1到多个字符

^ 字符串开始标志

$ 字符串结束标志

\n 转义符标志

反向引用 $N 用于 RewriteRule 中匹配的变量调用(0 <= N <= 9)

反向引用 %N 用于 RewriteCond 中最后一个匹配的变量调用(1 <= N <= 9)

5、实际操作:例子: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>


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地址会改变成新的地址,而使用内部跳转则浏览器中的地址不发生改变,看上去更像实际的二级域名虚拟服务器。

这样设置后,重启Apache服务器,测试一下,就大功告成了!