正在加载...
2007-4
29
发表于: 未分类 | 作者: xurenlu
modrewrite配置文档学习笔记:

1.环境变量:

	例子:

	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
	描述:设定基于目录的重写规则的Base url.
	Syntax:	RewriteBase URL-path
	Context:	directory, .htaccess
	Override:	FileInfo
	Status:	Extension
	Module:	mod_rewrite
	这个一般不太需要管它.
3.RewriteCond
	用法:	RewriteCond Teststring Pattern
	这里是Teststring里用到的一些内置变量.
	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

	#你可以在规则前加"!"取反.
	#这是一些常见的特殊的规则。你可以使用他们来取代一些表达式:

	* 'CondPattern' (lexicographically follows)
    * '=CondPattern' (lexicographically equal)
    * '-d' (is directory)  测试是否是目录.
    * '-f' (is regular file) 测试是否是常规文件.
    * '-s' (is regular file, with size)
    * '-l' (is symbolic link)
    * '-F' (is existing file, via subrequest)
    * '-U' (is existing URL, via subrequest)

	你可以为规则后设置[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...

	如下例:
	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类的浏览器访问时会带你到homepage.max.html,这里支持所有特性。用Lynx来浏览时,带你到纯文本页(homepage.min.html),默认情况下会访问一个叫homepage.std.html的页.

4.RewriteEngine:
	使用:
	RewriteEngine On|Off
	如果存在.htaccess的文件,应当显式地指明RewriteEngine On.
	因为默认情况下.htaccess的RewriteEngine项是Off的。因此如果想用.htaccess来设置rewrite,文件头一行一般都需要RewriteEngine On

5.RewriteLock:
	供rewrite-mapping程序同步使用的锁文件。
	使用:
	RewriteLock file-path

6.RewriteLog:
	设定日志文件.如果不是以"/"开头,则是指相对ServerRoot的相对路径.
	使用:
	RewriteLog	file-path

7.RewriteLogLevel:
	设定日志记录级别。
	使用:
	RewriteLogLevel level
	默认:

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文件的格式有这个几个:
	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
		这里提供了一个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+)/);
		}

		untie %DB;
		close(TXT);

		用法:
		$ txt2dbm map.txt map.db 

	4.内置函数:
		map:int map-source:internal apache functions
		functions有:toupper,tolower,escape,unescape

	5.外部程序:
		maptyp:prg map source:外部程序的路径.
		示例:
		#!/usr/bin/perl
		$| = 1;
		while () {
			# ...put here any transformations or lookups...
			print $_;
		}

9.RewriteOptions
	用法:
	RewriteOptions rewriteoptions
	default	:
	rewriteoptions	maxRedirects=10
	上下文:
	server config,virtual host,.htaccess directory
	rewriteoptions可以是下面两个值:
	inherit:从上一级配置继承.
	MaxRedirects=num(10,15,,,,,等等)

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)

	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})

	关于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

	# 'qsappend|QSA' (query string append)
	# 'redirect|R [=code]' (force redirect)
	# 'skip|S=num' (skip next rule(s))
	# 'type|T=MIME-type' (force MIME type)

: http://www.162cm.com/archives/404.html

本文相关评论 - 1条评论都没有呢

还没有评论。