你可以为规则后设置[flags]选项作为第三个参数.常见的有:
You can also set special flags for CondPattern by appending [flags] as the third argument to the RewriteCond directive, where flags is a comma-separated list of any of the following flags:
# ‘nocase|NC’ (no case)
# ‘ornext|OR’ (or next condition)
例如:
RewriteCond %{REMOTE_HOST} ^host1.* [OR]
RewriteCond %{REMOTE_HOST} ^host2.* [OR]
RewriteCond %{REMOTE_HOST} ^host3.*
RewriteRule …some special stuff for any of these hosts…
4.RewriteEngine:
使用:
RewriteEngine On|Off
如果存在.htaccess的文件,应当显式地指明RewriteEngine On.
因为默认情况下.htaccess的RewriteEngine项是Off的。因此如果想用.htaccess来设置rewrite,文件头一行一般都需要RewriteEngine On
3.dbm
这里提供了一个txt2dmb程序。
!/path/to/bin/perl
##
## txt2dbm — convert txt map to dbm format
##
use NDBM_File;
use Fcntl;
($txtmap, $dbmmap) = @ARGV;
open(TXT, “<$txtmap") or die "Couldn't open $txtmap!n";
tie (%DB, 'NDBM_File', $dbmmap,O_RDWR|O_TRUNC|O_CREAT, 0644)
or die "Couldn't create $dbmmap!n";
while () {
next if (/^s*#/ or /^s*$/);
$DB{$1} = $2 if (/^s*(S+)s+(S+)/);
}
10.RewriteRule:
这是mod_rewrite中用得最多的语句了(”the real rewriting workhorse”);
用法:
RewriteRule pattern Substitution [Flags]
Quantifiers:
? 0 or 1 occurrences of the preceding text
* 0 or N occurrences of the preceding text (N > 0)
+ 1 or N occurrences of the preceding text (N > 1)
Grouping:
(text) Grouping of text
(used either to set the borders of an alternative as above, or
to make backreferences, where the Nth group can
be referred to on the RHS of a RewriteRule as $N)
Escaping:
char escape the given char
(for instance, to specify the chars “.[]()” etc.)
一些技巧:
1. back-references ($N) to the RewriteRule pattern
2. back-references (%N) to the last matched RewriteCond pattern
3. server-variables as in rule condition test-strings (%{VARNAME})
4. mapping-function calls (${mapname:key|default})
关于Flags:
# ‘chain|C’ (chained with next rule)
# ‘cookie|CO=NAME:VAL:domain[:lifetime[:path]]’ (set cookie)
# ‘env|E=VAR:VAL’ (set environment variable)
# ‘forbidden|F’ (force URL to be forbidden)
# ‘gone|G’ (force URL to be gone)
# ‘last|L’ (last rule)
# ‘next|N’ (next round)
# ‘nocase|NC’ (no case)
# ‘noescape|NE’ (no URI escaping of output)
例如:
RewriteRule /foo/(.*) /bar?arg=P1%3d$1 [R,NE]
这将会将’/foo/zed’ 转向到’/bar?arg=p1=zed’
# ‘nosubreq|NS’ ( not for internal sub-requests)
# ‘proxy|P’ (force proxy)
Note: mod_proxy must be enabled in order to use this flag.
# ‘passthrough|PT’ (pass through to next handler)
(例如:
RewriteRule ^/abc(.*) /def$1 [PT]
Alias /def /ghi