注意: 由于各人的服务器的配置都有所不同,你可能要更改设定来测试以下例子,例如使用mod_alias和mod_userdir时要加上[PT],或者使用.htaccess来重定向而非主设定文件等,请尽量理解各例子如何运作,不要生吞活剥地背诵。
|
/~user
」换成正规的「
/u/user
」,并且加上「/」号结尾。.
RewriteRule ^/~([^/]+)/?(.*) /u/$1/$2[R] RewriteRule ^/([uge])/([^/]+)___FCKpd___1nbsp;/$1/$2/ [R] |
RewriteCond %{HTTP_HOST} !^fully\.qualified\.domain\.name [NC] RewriteCond %{HTTP_HOST} !^$ RewriteCond %{SERVER_PORT} !^80$ RewriteRule ^/(.*) http://fully.qualified.domain.name:%{SERVER_PORT}/$1 [L,R] RewriteCond %{HTTP_HOST} !^fully\.qualified\.domain\.name [NC] RewriteCond %{HTTP_HOST} !^$ RewriteRule ^/(.*) http://fully.qualified.domain.name/$1 [L,R] |
/e/www/
(WWW的主目录)和
/e/sww/
(内联网的主目录)等等,因为所有的网页资料都放在
/e/www/
目录内,我们要确定所有内嵌的图像都能正确显示。
/
」重定向至「
/e/www/
」,用mod_rewrite来解决比用mod_alias来解决更为简洁,因为URL
别名只会比较
URL
的前部分,但重定向因可能涉及另一台服务器而需要不同的前缀部分(前缀部分已受DocumentRoot限制),所以mod_rewrite是最好的解决方法::
RewriteEngine on RewriteRule ^/$/e/www/[R] |
/~quux/foo
去寻找
foo
这个档案,而非显示这个目录。其实很多时候,这问题应留待用户自己加「/」去解决,但有时你也可以完成步骤。例如你做了多次URL重定向,而目的地为一个CGI程序。
/~quux/foo/index.html
的
image.gif
时,重定向后会变成
/~quux/image.gif
。
RewriteEngineon RewriteBase /~quux/ RewriteRule ^foo$foo/[R] |
RewriteEngineon RewriteBase /~quux/ RewriteCond %{REQUEST_FILENAME}-d RewriteRule ^(.+[^/])___FCKpd___17nbsp; $1/[R] |
user1server_of_user1
user2server_of_user2
: :
map.xxx-to-host
。然后指示服务器把URL重定向,由
/u/user/anypath
/g/group/anypath
/e/entity/anypath
http://physical-host/u/user/anypath
http://physical-host/g/group/anypath
http://physical-host/e/entity/anypath
RewriteEngine on RewriteMap user-to-host txt:/path/to/map.user-to-host RewriteMap group-to-host txt:/path/to/map.group-to-host RewriteMap entity-to-host txt:/path/to/map.entity-to-host RewriteRule ^/u/([^/]+)/?(.*) http://${user-to-host:$1|server0}/u/$1/$2 RewriteRule ^/g/([^/]+)/?(.*)http://${group-to-host:$1|server0}/g/$1/$2 RewriteRule ^/e/([^/]+)/?(.*) http://${entity-to-host:$1|server0}/e/$1/$2 RewriteRule ^/([uge])/([^/]+)/?___FCKpd___37nbsp; /$1/$2/.www/ RewriteRule ^/([uge])/([^/]+)/([^.]+.+) /$1/$2/.www/$3\ |
/~user/anypath
URL重定向至
http://newserver/~user/anypath
。
RewriteEngine on RewriteRule ^/~(.+)http://newserver/~$1[R,L] |
/~foo/anypath
将会是
/home/
f
/foo/.www/anypath
,而
/~bar/anypath
就是
/home/
b
/bar/.www/anypath
。
RewriteEngine on RewriteRule ^/~(([a-z])[a-z0-9]+)(.*)/home/$2/$1/.www$3 |
RewriteRules
来显示整个目录结构。背景:
net.sw是一个装满Unix免费软件的资料夹,并以下列结构存储:
drwxrwxr-x 2 netswusers 512 Aug3 18:39 Audio/
drwxrwxr-x 2 netswusers 512 Jul9 14:37 Benchmark/
drwxrwxr-x12 netswusers 512 Jul9 00:34 Crypto/
drwxrwxr-x 5 netswusers 512 Jul9 00:41 Database/
drwxrwxr-x 4 netswusers 512 Jul 30 19:25 Dicts/
drwxrwxr-x10 netswusers 512 Jul9 01:54 Graphic/
drwxrwxr-x 5 netswusers 512 Jul9 01:58 Hackers/
drwxrwxr-x 8 netswusers 512 Jul9 03:19 InfoSys/
drwxrwxr-x 3 netswusers 512 Jul9 03:21 Math/
drwxrwxr-x 3 netswusers 512 Jul9 03:24 Misc/
drwxrwxr-x 9 netswusers 512 Aug1 16:33 Network/
drwxrwxr-x 2 netswusers 512 Jul9 05:53 Office/
drwxrwxr-x 7 netswusers 512 Jul9 09:24 SoftEng/
drwxrwxr-x 7 netswusers 512 Jul 9 12:17 System/
drwxrwxr-x12 netswusers 512 Aug3 20:15 Typesetting/
drwxrwxr-x10 netswusers 512 Jul9 14:08 X11/
/e/netsw/.www/
:
-rw-r--r-- 1 netswusers 1318 Aug1 18:10 .wwwacl
drwxr-xr-x18 netswusers 512 Aug5 15:51 DATA/
-rw-rw-rw- 1 netswusers372982 Aug5 16:35 LOGFILE
-rw-r--r-- 1 netswusers 659 Aug4 09:27 TODO
-rw-r--r-- 1 netswusers 5697 Aug1 18:01 netsw-about.html
-rwxr-xr-x 1 netswusers 579 Aug2 10:33 netsw-access.pl
-rwxr-xr-x 1 netswusers 1532 Aug1 17:35 netsw-changes.cgi
-rwxr-xr-x 1 netswusers 2866 Aug5 14:49 netsw-home.cgi
drwxr-xr-x 2 netswusers 512 Jul8 23:47 netsw-img/
-rwxr-xr-x 1 netswusers 24050 Aug5 15:49 netsw-lsdir.cgi
-rwxr-xr-x 1 netswusers 1589 Aug3 18:43 netsw-search.cgi
-rwxr-xr-x 1 netswusers 1885 Aug1 17:41 netsw-tree.cgi
-rw-r--r--1 netswusers 234 Jul 30 16:35 netsw-unlimit.lst
DATA/
子目录就是刚才的资料夹,
net.sw内的软件会经
rdist
程序来自动更新。第二部份将这资料夹和新建立的CGI、网页配合,我们想将
DATA/
稳藏起来,而在用户请求不同URL时执行正确的CGI程序来显示。先将
/net.sw/
这URL重定向至
/e/netsw
:
RewriteRule^net.sw$ net.sw/ [R] RewriteRule^net.sw/(.*)___FCKpd___73nbsp;e/netsw/$1 |
/e/netsw/.www/.wwwacl
:
Options ExecCGI FollowSymLinks Includes MultiViews RewriteEngine on #we are reached via /net.sw/ prefix RewriteBase /net.sw/ #first we rewrite the root dir to #the handling cgi script RewriteRule ^___FCKpd___83nbsp; netsw-home.cgi [L] RewriteRule ^index\.html___FCKpd___84nbsp; netsw-home.cgi [L] #strip out the subdirs when #the browser requests us from perdir pages RewriteRule ^.+/(netsw-[^/]+/.+)___FCKpd___88nbsp; $1 [L] #and now break the rewriting for local files RewriteRule ^netsw-home\.cgi.* - [L] RewriteRule ^netsw-changes\.cgi.* - [L] RewriteRule ^netsw-search\.cgi.* - [L] RewriteRule ^netsw-tree\.cgi___FCKpd___94nbsp; - [L] RewriteRule ^netsw-about\.html___FCKpd___95nbsp; - [L] RewriteRule ^netsw-img/.*___FCKpd___96nbsp; - [L] #anything else is a subdir which gets handled #by another cgi script RewriteRule !^netsw-lsdir\.cgi.* - [C] RewriteRule (.*) netsw-lsdir.cgi/$1 |
imagemap
顺利转换到Apache的
mod_imap
,问题是
imagemap
已被很多超级链接连系着,但旧的
imagemap
是存储在
/cgi-bin/imagemap/path/to/page.map
,而在Apache却是放在
/path/to/page.map
。
/cgi-bin/
」移除便可:
RewriteEngineon RewriteRule ^/cgi-bin/imagemap(.*)$1[PT] |
RewriteEngine on # first try to find it in custom/... # ...and if found stop and be happy: RewriteCond /your/docroot/dir1/%{REQUEST_FILENAME}-f RewriteRule^(.+)/your/docroot/dir1/$1[L] # second try to find it in pub/... # ...and if found stop and be happy: RewriteCond /your/docroot/dir2/%{REQUEST_FILENAME}-f RewriteRule^(.+)/your/docroot/dir2/$1[L] # else go on for other Alias or ScriptAlias directives, # etc. RewriteRule ^(.+)-[PT] |
/foo/S=java/bar/
转换为
/foo/bar/
,然后把「java」写入环境变量「
STATUS
」。
RewriteEngine on RewriteRule ^(.*)/S=([^/]+)/(.*) $1/$3 [E=STATUS:$2] |
www.
username
.host.domain.com
的请求直接对映到档案系统,放弃使用Apache的虚拟主机功能。
http://www.username.host.com/anypath
重定向到
/home/username/anypath
:
RewriteEngine on RewriteCond %{HTTP_HOST} ^www\.[^.]+\.host\.com$ RewriteRule ^(.+) %{HTTP_HOST}$1 [C] RewriteRule ^www\.([^.]+)\.host\.com(.*) /home/$1$2 |
ourdomain.com
时,就将请求重定向至
www.somewhere.com</CODE。< dd>
RewriteEngine on RewriteCond %{REMOTE_HOST}!^.+\.ourdomain\.com$ RewriteRule ^(/~.+) http://www.somewhere.com/$1 [R,L] |
RewriteEngine on RewriteCond /your/docroot/%{REQUEST_FILENAME} !-f RewriteRule ^(.+) http://webserverB.dom/$1 |
RewriteEngine on RewriteCond %{REQUEST_URI} !-U RewriteRule ^(.+) http://webserverB.dom/$1 |
RewriteRule ^xredirect:(.+) /path/to/nph-xredirect.cgi/$1 \ [T=application/x-httpd-cgi,L] |
#!/path/to/perl ## ##nph-xredirect.cgi -- NPH/CGI script for extended redirects ##Copyright (c) 1997 Ralf S. Engelschall, All Rights Reserved. ## $| = 1; $url = $ENV{'PATH_INFO'}; print "HTTP/1.0 302 Moved Temporarily\n"; print "Server: $ENV{'SERVER_SOFTWARE'}\n"; print "Location: $url\n"; print "Content-type: text/html\n"; print "\n"; print "<html>\n"; print "<head>\n"; print "<title>302 Moved Temporarily (EXTENDED)</title>\n"; print "</head>\n"; print "<body>\n"; print "<h1>Moved Temporarily (EXTENDED)</h1>\n"; print "The document has moved <a HREF=\"$url\">here</a>.<p>\n"; print "</body>\n"; print "</html>\n"; ##EOF## |
RewriteRule ^anyurlxredirect:news:newsgroup |
RewriteEngine on RewriteMap multiplex txt:/path/to/map.cxan RewriteRule ^/CxAN/(.*) %{REMOTE_HOST}::$1 [C] RewriteRule ^.+\.([a-zA-Z]+)::(.*)___FCKpd___165nbsp;${multiplex:$1|ftp.default.dom}$2[R,L] |
## ##map.cxan -- Multiplexing Map for CxAN ## de ftp://ftp.cxan.de/CxAN/ uk ftp://ftp.cxan.uk/CxAN/ com ftp://ftp.cxan.com/CxAN/ : ##EOF## |
TIME_xxx
开始的环境变量,将这些时间环境变量进行字符串比较可决定重定向至哪个网页:
RewriteEngine on RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700 RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900 RewriteRule ^foo\.html___FCKpd___178nbsp; foo.day.html RewriteRule ^foo\.html___FCKpd___179nbsp; foo.night.html |
foo.day.html
,其余时间则显示foo.html
# backward compatibility ruleset for # rewriting document.html to document.phtml # when and only when document.phtml exists # but no longer document.html RewriteEngine on RewriteBase /~quux/ # parse out basename, but remember the fact RewriteRule ^(.*)\.html___FCKpd___187nbsp; $1 [C,E=WasHTML:yes] # rewrite to document.phtml if exists RewriteCond %{REQUEST_FILENAME}.phtml -f RewriteRule ^(.*)$ $1.phtml [S=1] # else reverse the previous basename cutout RewriteCond %{ENV:WasHTML} ^yes$ RewriteRule^(.*)$ $1.html |
bar.html
改名为
foo.html
,而我们又想保留旧有的URL,甚至不想给用户新的URL去连至这新档案。
RewriteEngineon RewriteBase /~quux/ RewriteRule ^foo\.html___FCKpd___196nbsp;bar.html |
bar.html
改名为
foo.html
,但这次我们想直接将用户的网页重定向至新的文件,即浏览器的URL位置有所改变。
RewriteEngineon RewriteBase /~quux/ RewriteRule ^foo\.html___FCKpd___199nbsp;bar.html[R] |
RewriteCond %{HTTP_USER_AGENT}^Mozilla/3.* RewriteRule ^foo\.html$ foo.NS.html [L] RewriteCond %{HTTP_USER_AGENT}^Lynx/.* [OR] RewriteCond %{HTTP_USER_AGENT}^Mozilla/[12].* RewriteRule ^foo\.html$ foo.20.html [L] RewriteRule ^foo\.html$ foo.32.html [L] |
mirror
程序将最新的档案移到自己的主机上,我们可用webcopy经网页服务器HTTP把档案下载,但这方法有一坏处:只有在执行webcopy时才能更新档案。更好的办法就是在发出请求时立刻找寻最新的档案来源,然后实时下载到自己主机中。
RewriteEngine on RewriteBase /~quux/ RewriteRule ^hotsheet/(.*)___FCKpd___210nbsp;http://www.tstimpreso.com/hotsheet/$1[P] |
RewriteEngineon RewriteBase /~quux/ RewriteRule ^usa-news\.html___FCKpd___213nbsp; http://www.quux-corp.com/news/index.html[P] |
RewriteEngine on RewriteCond /mirror/of/remotesite/$1 -U RewriteRule ^http://www\.remotesite\.com/(.*)$ /mirror/of/remotesite/$1 |
www.quux-corp.dom
),第二个则是内部使用,受防火墙所保护,一切资料及网站维护都经这个服务器进行,现在我们想令外部服务器能存取穿过防火墙,获取内部服务器已最新的档案。
ALLOW Host www.quux-corp.dom Port >1024 --> Host www2.quux-corp.dom Port 80 DENYHost * Port * --> Host www2.quux-corp.dom Port 80 |
RewriteRule ^/~([^/]+)/?(.*) /home/$1/.www/$2 RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^/home/([^/]+)/.www/?(.*) http://www2.quux-corp.dom/~$1/pub/$2 [P] |
www[0-5].foo.com
这六部服务器的工作量平均分配。
www0 INA 1.2.3.1 www1INA 1.2.3.2 www2 INA 1.2.3.3 www3 INA 1.2.3.4 www4 INA 1.2.3.5 www5 INA 1.2.3.6 |
www INCNAME www0.foo.com. INCNAME www1.foo.com. INCNAME www2.foo.com. INCNAME www3.foo.com. INCNAME www4.foo.com. INCNAME www5.foo.com. INCNAME www6.foo.com. |
www.foo.com
的解析请求,然后BIND就会循环地解析作www0-www6
,这样就能将用户分配到不同的服务器上,但请记得这不是一个完美的方案,因为其它的域名服务器会快取你服务器的域名解析结果,所以每一次解析到wwwX.foo.com
时,都会有很多用户同时被派往同一部服务器,但整体来说已能平衡各服务器的负荷。
lbnamed
程序专责利用域名服务器把用户请求分发到不同的服务器上,这是一个用Perl 5及其它附助工具写的复杂DNS工作量分配程序。
www0.foo.com
即是www.foo.com
的记录。
www INCNAME www0.foo.com. |
www0.foo.com
变为一独立代理服务器,即是建立一专责代理服务器,然后把请求分流至五部不同的服务器(www1-www5
),我们用lb.pl及以下mod_rewrite规则:
RewriteEngine on RewriteMap lb prg:/path/to/lb.pl RewriteRule ^/(.+)$ ${lb:$1} [P,L] |
lb.pl
的程序代码:
#!/path/to/perl ## ##lb.pl -- load balancing script ## $| = 1; $name = "www"; # the hostname base $first= 1; # the first server (not 0 here, because 0 is myself) $last = 5; # the last server in the round-robin $domain = "foo.dom"; # the domainname $cnt = 0; while (<STDIN>) { $cnt = (($cnt+1) % ($last+1-$first)); $server = sprintf("%s%d.%s", $name, $cnt+$first, $domain); print "http://$server/
|
评论