- 論壇徽章:
- 0
|
modrewrite配置文檔學(xué)習(xí)筆記:
1.環(huán)境變量:
例子:
SCRIPT_NAME=/sw/lib/w3s/tree/global/u/rse/.www/index.html
SCRIPT_FILENAME=/u/rse/.www/index.html
SCRIPT_URL=/u/rse/
SCRIPT_URI=http://en1.engelschall.com/u/rse/
2:RewriteBase
描述:設(shè)定基于目錄的重寫規(guī)則的Base url.
Syntax: RewriteBase URL-path
Context: directory, .htaccess
Override: FileInfo
Status: Extension
Module: mod_rewrite
這個(gè)一般不太需要管它.
3.RewriteCond
用法: RewriteCond Teststring Pattern
這里是Teststring里用到的一些內(nèi)置變量.
HTTP headers: connection & request:
HTTP_USER_AGENT
HTTP_REFERER
HTTP_COOKIE
HTTP_FORWARDED
HTTP_HOST
HTTP_PROXY_CONNECTION
HTTP_ACCEPT
REMOTE_ADDR
REMOTE_HOST
REMOTE_PORT
REMOTE_USER
REMOTE_IDENT
REQUEST_METHOD
SCRIPT_FILENAME
PATH_INFO
QUERY_STRING
AUTH_TYPE
DOCUMENT_ROOT
SERVER_ADMIN
SERVER_NAME
SERVER_ADDR
SERVER_PORT
SERVER_PROTOCOL
SERVER_SOFTWARE
TIME_YEAR
TIME_MON
TIME_DAY
TIME_HOUR
TIME_MIN
TIME_SEC
TIME_WDAY
TIME
API_VERSION
THE_REQUEST
REQUEST_URI
REQUEST_FILENAME
IS_SUBREQ
HTTPS
#你可以在規(guī)則前加"!"取反.
#這是一些常見(jiàn)的特殊的規(guī)則。你可以使用他們來(lái)取代一些表達(dá)式:
* 'CondPattern’ (lexicographically follows)
* ‘=CondPattern’ (lexicographically equal)
* ‘-d’ (is directory) 測(cè)試是否是目錄.
* ‘-f’ (is regular file) 測(cè)試是否是常規(guī)文件.
* ‘-s’ (is regular file, with size)
* ‘-l’ (is symbolic link)
* ‘-F’ (is existing file, via subrequest)
* ‘-U’ (is existing URL, via subrequest)
你可以為規(guī)則后設(shè)置[flags]選項(xiàng)作為第三個(gè)參數(shù).常見(jiàn)的有:
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…
如下例:
RewriteCond %{HTTP_USER_AGENT} ^Mozilla.*
RewriteRule ^/$ /homepage.max.html [L]
RewriteCond %{HTTP_USER_AGENT} ^Lynx.*
RewriteRule ^/$ /homepage.min.html [L]
RewriteRule ^/$ /homepage.std.html [L]
用Mozilla類的瀏覽器訪問(wèn)時(shí)會(huì)帶你到homepage.max.html,這里支持所有特性。用Lynx來(lái)瀏覽時(shí),帶你到純文本頁(yè)(homepage.min.html),默認(rèn)情況下會(huì)訪問(wèn)一個(gè)叫homepage.std.html的頁(yè).
4.RewriteEngine:
使用:
RewriteEngine On|Off
如果存在.htaccess的文件,應(yīng)當(dāng)顯式地指明RewriteEngine On.
因?yàn)槟J(rèn)情況下.htaccess的RewriteEngine項(xiàng)是Off的。因此如果想用.htaccess來(lái)設(shè)置rewrite,文件頭一行一般都需要RewriteEngine On
5.RewriteLock:
供rewrite-mapping程序同步使用的鎖文件。
使用:
RewriteLock file-path
6.RewriteLog:
設(shè)定日志文件.如果不是以”/”開(kāi)頭,則是指相對(duì)ServerRoot的相對(duì)路徑.
使用:
RewriteLog file-path
7.RewriteLogLevel:
設(shè)定日志記錄級(jí)別。
使用:
RewriteLogLevel level
默認(rèn):
8.RewriteMap:
使用Mapping.
它的上下文是Server config 和Virtual host
使用:
RewriteMap Mapname MapType:Mapsource
例如:
在httpd.conf中配置:
#RewriteMap exampleMap txt:/path/to/file/map.txt
#RewriteRule ^/ex/(.*) ${exampleMap:$1}
而map文件的格式有這個(gè)幾個(gè):
1.txt類型的map文件:
#Standard Plain txt:
#map.txt
#maptype:txt
Ralf.S.Engelschall rse # Bastard Operator From Hell
Mr.Joe.Average joe # Mr. Average
2.Randomized Plain Text
#maptyp:rnd
static www1|www2|www3|www4
dynamic www5|www6
配置:
RewriteMap servers rnd:/path/to/mapfile/map.txt
RewriteRule ^/(.*.(png|gif|jpg)) http://${servers:static}/$1 [NC,P,L]
RewriteRule ^/(.*) http://${servers:dynamic}/$1 [P,L]
3.dbm
這里提供了一個(gè)txt2dmb程序。
!/path/to/bin/perl
##
## txt2dbm — convert txt map to dbm format
##
use NDBM_File;
use Fcntl;
($txtmap, $dbmmap) = @ARGV;
open(TXT, “配置繼承.
MaxRedirects=num(10,15,,,,,等等)
10.RewriteRule:
這是mod_rewrite中用得最多的語(yǔ)句了(”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)
Anchors:
^ Start-of-line anchor
$ End-of-line anchor
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})
關(guān)于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]
這將會(huì)將’/foo/zed’ 轉(zhuǎn)向到’/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
# ‘qsappend|QSA’ (query string append)
# ‘redirect|R [=code]’ (force redirect)
# ’skip|S=num’ (skip next rule(s))
# ‘type|T=MIME-type’ (force MIME type)
本文來(lái)自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):http://blog.chinaunix.net/u/15067/showart_1192944.html |
|