Apache HTTP Server �汾2.2
Originally written by
Ralf S. Engelschall <rse@apache.org>
December 1997
������mod_rewrite
���ο��ĵ���������ʵ��Ӧ������ν�����������ٵĻ���URL�ĵ������⣬����ϸ�������������URL��д�����Խ�����⡣
mod_rewrite
���Apache��mod_rewrite
���ṩ��ǿ��URL������ɱ�ּ���ģ�飬����ʵ�ּ��������������URL�������ͣ�����������������临���ԣ���Ϊmod_rewrite
����Ҫ�ϰ����dz�ѧ�߲������������ã���ʹ��Apacheר����ʱҲ�ᷢ���mod_rewrite
������;�����仰˵������mod_rewrite
�������Ǵ����ù��������ã�������ϲ������һ�����á�������ͼͨ�������з����ı���������һ���ɹ��Ŀ��ˣ������������
���Լ�������ռ������ʵ����������Ҫ��η����������Щ���ӿ�ʼѧϰURL��д�ĺ�ϻ�Ӱɡ�
mod_alias
��mod_userdir
ʱҪ����[PT]��־��������д.htaccess�����ǵ����������еĹ�����һ���ض��Ĺ���Ӧ���������Ȼ���ٿ���Ӧ�ã��������ܱ���������⡣����Щweb�������ϣ�һ����Դ��ӵ�ж��URL����ʵ��Ӧ�úͷ�����Ӧ�ñ�ʹ�õ��ǹ淶��URL�����������Ǽ�д����ֻ���ڲ�ʹ�á������û���������ʹ��ʲô��ʽ��URL�������տ����Ķ�Ӧ���ǹ淶��URL��
�����в��淶��URLִ��һ���ⲿHTTP�ض����Ըı������������ַ���е���ʾ���������������еĹ����ù淶��/u/user�滻/~user����������/u/user����©�ĺ�б�ܡ�
RewriteRule ^/~([^/]+)/?(.*) /u/$1/$2 [R] RewriteRule ^/([uge])/([^/]+)$ /$1/$2/ [R]
# ��������ڷ�80�˿ڵ�վ�� 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] # ��һ��������80�˿ڵ�վ�� RewriteCond %{HTTP_HOST} !^fully\.qualified\.domain\.name [NC] RewriteCond %{HTTP_HOST} !^$ RewriteRule ^/(.*) http://fully.qualified.domain.name/$1 [L,R]
DocumentRoot
ͨ����web��������DocumentRoot
ֱ�Ӷ�Ӧ��URL"/"�����ǣ����������Ǵ������һ����������ֻ���ڶ����ݳ��е�һ��ʵ�塣���磬��Intranetվ���У���/e/www/(WWW����ҳ)��/e/sww/(Intranet����ҳ)�ȵȣ���DocumentRoot
ָ����/e/www/������뱣֤�����ݳ���������Ƕ��ͼƬ������Ԫ�ضԺ��������Ч��
ֻ���ض���URL"/"��"/e/www/"���ɡ���������������ܼ���ֻ����Ϊ����mod_rewriteģ���֧�֣����ż���Ϊ��ͳ��URL Aliases����(��mod_alias�������ģ���ṩ)ֻ������һ��ǰƥ�䣬DocumentRoot��һ��������URL��ǰ�������ʵ���������ض�����mod_rewrite��ȷ�ܼ�
RewriteEngine on RewriteRule ^/$ /e/www/ [R]
ע�⣬ Ҳ����ͨ��RedirectMatch
ָ��ﵽ���Ŀ�ģ�
RedirectMatch ^/$ http://example.com/e/www/
ÿ�����ܶ�����Ŀ¼��б�ܵ����ⶼ��һ���ྭ�������©�ˣ������������һ��������Ϊ���������/~quux/foo������/~quux/foo/ ���������ͻ�ȥ��һ����foo���ļ���������һ��Ŀ¼�����Ծͱ����ˡ���ʵ�ϣ����������£����Լ�����ͼ���������������ʱ����Ҫ���ֹ����������磬����д�����CGI�ű��еĸ��ӵ�URL�Ժ�
������������ķ������÷������Զ���Ӻ�б�ܡ��Դˣ�����ʹ��һ���ⲿ�ض���ʹ�������ȷ�ش����̵Ķ�����ͼƬ���������������һ���ڲ���д������ֻ��Ŀ¼ҳ����Ч��������Ƕ��ʹ�����URL��ͼƬ��ҳ����Ч����Ϊ�������������ǶĿ��Ŀ��ܡ����磬��������ⲿ�ض���/~quux/foo/index.htmlҳ���ж�image.gif��������������/~quux/image.gif
���ԣ�Ӧ������д��
RewriteEngine on RewriteBase /~quux/ RewriteRule ^foo$ foo/ [R]
�����ַ��������ǰ���Щд��������Ŀ¼�еĶ���.htaccess�У�������ע�⣬��˻����һЩ�����ϵĿ�����
RewriteEngine on RewriteBase /~quux/ RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^(.+[^/])$ $1/ [R]
����ϣ����һ��Intranet��Ⱥ��վ�У�������WWW����������һ�µ�URL�滮��Ҳ����˵�����е�URL(���ÿ�����������б������ã�����Ƕ����ڸ�����������)ʵ���϶��Ƕ����ڸ����������ģ�������Ҫ����һ�����ж����ڸ�����������һ���Թ滮��WWW���ƿռ䣬��URL����Ҫ��������Ŀ������������ɼ�Ⱥ�������Զ���λ����Ŀ��������
���ȣ�Ŀ�����������Ϣ����(����)�ڰ������û������Լ�ʵ����ⲿ��ͼ�����ʽ���磺
user1 server_of_user1 user2 server_of_user2 : :
��Щ��Ϣ������map.xxx-to-host�ļ�����Σ����URL��һ������������Ч����Ҫ�������еķ������ض���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
���¹�������ӳ���ļ�������������(�ٶ������һ���û���ӳ����û�ж�Ӧ�����ʹ��server0ΪĬ�Ϸ�����)��
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])/([^/]+)/?$ /$1/$2/.www/ RewriteRule ^/([uge])/([^/]+)/([^.]+.+) /$1/$2/.www/$3\
ͨ������������ڽ���һ���µ�web������ʱ��������������Ҫ���ض���һ��web�������ϵ������û���Ŀ¼����һ��web��������
�ܼ����ϵ�web���������ض������е�URL"/~user/anypath"��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
����һ�����ӵ��������ӣ�һ������ʹ�����Ŀ¼�Ĺ�����ʵ��ƽ���Ĺ۸У����Ҵ������õ������ݽṹ��ɱ�ּ���Ӧ�á�������net.sw��1992�꿪ʼ����������ռ������Unix������������ҵİ���Ҳ���ҵĹ�������Ϊ��ѧϰ�������ѧ��ͬʱ��ҵ��ʱ�仹���˶����ϵͳ���������Ա��ÿ���Ҷ���Ҫ������������������һ����κ����Ŀ¼�ṹ����Ÿ����������
drwxrwxr-x 2 netsw users 512 Aug 3 18:39 Audio/ drwxrwxr-x 2 netsw users 512 Jul 9 14:37 Benchmark/ drwxrwxr-x 12 netsw users 512 Jul 9 00:34 Crypto/ drwxrwxr-x 5 netsw users 512 Jul 9 00:41 Database/ drwxrwxr-x 4 netsw users 512 Jul 30 19:25 Dicts/ drwxrwxr-x 10 netsw users 512 Jul 9 01:54 Graphic/ drwxrwxr-x 5 netsw users 512 Jul 9 01:58 Hackers/ drwxrwxr-x 8 netsw users 512 Jul 9 03:19 InfoSys/ drwxrwxr-x 3 netsw users 512 Jul 9 03:21 Math/ drwxrwxr-x 3 netsw users 512 Jul 9 03:24 Misc/ drwxrwxr-x 9 netsw users 512 Aug 1 16:33 Network/ drwxrwxr-x 2 netsw users 512 Jul 9 05:53 Office/ drwxrwxr-x 7 netsw users 512 Jul 9 09:24 SoftEng/ drwxrwxr-x 7 netsw users 512 Jul 9 12:17 System/ drwxrwxr-x 12 netsw users 512 Aug 3 20:15 Typesetting/ drwxrwxr-x 10 netsw users 512 Jul 9 14:08 X11/
1996��7�£��Ҿ���ͨ��һ��Ư����Web�ӿڹ����ҵ��ղء�"Ư��"��ָ�ṩһ���ӿ���ֱ���������Ŀ¼�ṹ��ͬʱ��������ṹ���κθı䣬����Ҳ���ڽṹ��������CGI�ű���Ϊʲô�أ���Ϊ����ṹ��Ҫ�ܹ���FTP���ʣ������Ҳ�ϣ���������κ�Web����CGI�ɷ֡�
���������Ϊ�������֣���һ�����֣��������ڿ���ʱ�佨������Ŀ¼ҳ���CGI�ű������Ұ����Ƿ���/e/netsw/.www/�����£�
-rw-r--r-- 1 netsw users 1318 Aug 1 18:10 .wwwacl drwxr-xr-x 18 netsw users 512 Aug 5 15:51 DATA/ -rw-rw-rw- 1 netsw users 372982 Aug 5 16:35 LOGFILE -rw-r--r-- 1 netsw users 659 Aug 4 09:27 TODO -rw-r--r-- 1 netsw users 5697 Aug 1 18:01 netsw-about.html -rwxr-xr-x 1 netsw users 579 Aug 2 10:33 netsw-access.pl -rwxr-xr-x 1 netsw users 1532 Aug 1 17:35 netsw-changes.cgi -rwxr-xr-x 1 netsw users 2866 Aug 5 14:49 netsw-home.cgi drwxr-xr-x 2 netsw users 512 Jul 8 23:47 netsw-img/ -rwxr-xr-x 1 netsw users 24050 Aug 5 15:49 netsw-lsdir.cgi -rwxr-xr-x 1 netsw users 1589 Aug 3 18:43 netsw-search.cgi -rwxr-xr-x 1 netsw users 1885 Aug 1 17:41 netsw-tree.cgi -rw-r--r-- 1 netsw users 234 Jul 30 16:35 netsw-unlimit.lst
���е�"DATA"��Ŀ¼����������Ŀ¼�ṹ����ʵ�ڵ�net.sw ����rdist����Ҫ��ʱ���Զ����¡��ڶ������ֵ����������ǣ���������������ṹΪһ��ƽ���۸е�URL������ϣ���������ʵ���CGI�ű���ʹ�ø���URL��ʱ��ʹ�û��о�����"DATA"Ŀ¼�Ĵ��ڡ��������£����ȣ��Ұ��������÷��ڷ�������DocumentRoot�����Ŀ¼�������ļ����д������URL"/net.sw/"Ϊ�ڲ�·��"/e/netsw" ��
RewriteRule ^net.sw$ net.sw/ [R] RewriteRule ^net.sw/(.*)$ e/netsw/$1
��һ�������������©��б�ܵ�����ģ��ڶ��������������ʵ�ֹ��ܵġ����ţ����Ƿ������Ŀ¼�������ļ�/e/netsw/.www/.wwwacl�е�ɱ�ּ��������ˣ�
Options ExecCGI FollowSymLinks Includes MultiViews RewriteEngine on # ����ͨ��"/net.sw/"ǰ���� RewriteBase /net.sw/ # ������д��Ŀ¼��cgi����ű� RewriteRule ^$ netsw-home.cgi [L] RewriteRule ^index\.html$ netsw-home.cgi [L] # �����������perdirҳ��ʱ��ȥ��Ŀ¼ RewriteRule ^.+/(netsw-[^/]+/.+)$ $1 [L] # ���ڴ�ϱ����ļ�����д RewriteRule ^netsw-home\.cgi.* - [L] RewriteRule ^netsw-changes\.cgi.* - [L] RewriteRule ^netsw-search\.cgi.* - [L] RewriteRule ^netsw-tree\.cgi$ - [L] RewriteRule ^netsw-about\.html$ - [L] RewriteRule ^netsw-img/.*$ - [L] # �κα�Ķ�������һ������һ��cgi�ű��������Ŀ¼ RewriteRule !^netsw-lsdir\.cgi.* - [C] RewriteRule (.*) netsw-lsdir.cgi/$1
�Ķ���ʾ��
mod_imap
����˶�ϣ���ڴ�NCSA web����������ִ���Apache web������ת����ʵ��ƽ�����ɣ���ϣ���ϵ�NCSAͼ��ӳ���������Apache�Ľ��ִ���mod_imagemap
֧�����������������������ڣ���������ͨ��/cgi-bin/imagemap/path/to/page.map����imagemap��������ӣ�����Apache�£�Ӧ��д��/path/to/page.map
ʹ��ȫ�ֹ����ڴ��������ȥ��������Щ�����ǰ��
RewriteEngine on RewriteRule ^/cgi-bin/imagemap(.*) $1 [PT]
��ʱ���б�Ҫʹweb�������ڶ��Ŀ¼������ҳ�棬�Դˣ�MultiViews����������������Ϊ����
����һ����ȷ�Ĺ���������Ŀ¼�е��ļ���
RewriteEngine on # ���ȳ����� custom/...��Ѱ�� RewriteCond /your/docroot/dir1/%{REQUEST_FILENAME} -f RewriteRule ^(.+) /your/docroot/dir1/$1 [L] # Ȼ������ pub/...��Ѱ�� RewriteCond /your/docroot/dir2/%{REQUEST_FILENAME} -f RewriteRule ^(.+) /your/docroot/dir2/$1 [L] # ���Ҳ����ͼ���Ѱ��������Alias �� ScriptAlias Ŀ¼... RewriteRule ^(.+) - [PT]
���ϣ����������֮���״̬��Ϣ���ֲ�ϣ��ʹ��CGI����װ����ҳ�棬����ֻͨ������URL�е�������Ϣ�����롣
������һ�������������״̬��Ϣ�������û��������Ա��˺�����XSSI��CGI ��������һ��"/foo/S=java/bar/"��URL�ᱻ����Ϊ/foo/bar/ ������������STATUS������Ϊ"java"��
RewriteEngine on RewriteRule ^(.*)/S=([^/]+)/(.*) $1/$3 [E=STATUS:$2]
�����ҪΪ�û�username֧��һ��www.username.host.domain.com����ҳ�����������ڴ˻����Ͻ����������ķ����������ý��ڴ˻���������һ��DNS��¼�ķ���ʵ�֡�
��HTTP/1.0������������ʵ�ֵģ����Ƕ�HTTP/1.1����HTTPͷ�а������������������������¹������ڲ�����д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�����������ߵ������ض������û���Ŀ¼URL����һ��web������www.somewhere.com ����ʱ��������Ҳ�������������������ö��С�
ֻ��һ����д������
RewriteEngine on RewriteCond %{REMOTE_HOST} !^.+\.ourdomain\.com$ RewriteRule ^(/~.+) http://www.somewhere.com/$1 [R,L]
�����дURL���ض����web������A��ʧ����������B����һ�����������⡣һ�㣬������Perlд��CGI�ű�ͨ��ErrorDocument
����������⣬����mod_rewrite
������������ע�⣬���ַ�����ִ��Ч�ʲ�����ErrorDocument
��CGI�ű�!
��һ�ַ���������õ����ܶ������Ƿ�ѣ��������С����ȫ��
RewriteEngine on RewriteCond /your/docroot/%{REQUEST_FILENAME} !-f RewriteRule ^(.+) http://webserverB.dom/$1
�������������ڣ���ֻ��λ��DocumentRoot
�е�ҳ����Ч����Ȼ�������Ӹ��������(����ͬʱ�������û���Ŀ¼���ȵ�)�����ǻ���һ�����õķ�����
RewriteEngine on RewriteCond %{REQUEST_URI} !-U RewriteRule ^(.+) http://webserverB.dom/$1
���ַ���ʹ����mod_rewrite
�ṩ��"��ǰ����"(look-ahead)�Ĺ��ܣ���һ�ֶ�����URL���Ͷ���Ч���Ұ�ȫ�ķ��������ǣ���web�����������ܻ���Ӱ�죬�������web��������һ��ǿ���CPU���Ǿ�������������������ٻ����ϣ������õ�һ�ַ��������������ܸ��õ�ErrorDocument
CGI�ű���
��ʱ�����ǻ���Ҫ����Ķ��ض���URL��(�й��ַ�ת����Ʒ����)���ơ�ͨ����Apache�ں��е�URLת�庯��uri_escape()ͬʱ�����ê(anchor)ת�壬������"url#anchor"��URL����ˣ��㲻����mod_rewrite
�Դ���URLֱ���ض�����ô���ʵ����?
������NPH-CGI�ű�ʹ���Լ��ض�����Ϊ��NPH(���������HTTPͷ)���ᷢ��ת����������ȣ�����Է�������������(Ӧ��λ��������д��������)������һ���µ�URL����"xredirect:"��
RewriteRule ^xredirect:(.+) /path/to/nph-xredirect.cgi/$1 \ [T=application/x-httpd-cgi,L]
��ǿ�����д�"xredirect:"ǰ��URL�����͵����µ�nph-xredirect.cgi����
#!/path/to/perl ## ## nph-xredirect.cgi -- NPH/CGI script for extended redirects ## $| = 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##
����һ�ֿ����ض�������URL���͵ķ�������������mod_rewrite
ֱ��֧�ֵ����͡����ԣ������������ض���"news:newsgroup"��
RewriteRule ^anyurl xredirect:news:newsgroup
��֪��http://www.perl.com/CPAN��CPAN(�ۺ�Perl�浵����)����ʵ����һ���ض������ṩȫ�����CPAN������������������һ��FTPվ�㣬Ҳ���Գ�֮ΪFTP���ʶ�·���÷���CPAN��ͨ��CGI�ű�ʵ�ֵģ���ô��mod_rewrite
���ʵ���أ�
���ȣ�����ע�mod_rewrite
��3.0.0�汾��ʼ����������д"ftp:"���͡���Σ��Կͻ��˶���������·���������ȡ������RewriteMap
ʵ�֡�������ʽ�������ö���������Ϊ���Ҷ�·���õ�ͼ�ļ���������������
RewriteEngine on RewriteMap multiplex txt:/path/to/map.cxan RewriteRule ^/CxAN/(.*) %{REMOTE_HOST}::$1 [C] RewriteRule ^.+\.([a-zA-Z]+)::(.*)$ ${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##
��ҳ�����ݰ�ʱ�䲻ͬ���仯�ij��ϣ������ض����ض�ҳ�棬���������Ȼ����CGI�ű��ķ����������mod_rewrite
��ʵ���أ�
���������TIME_xxx�ı�������������д�����У�����"<STRING", " >STRING"��"=STRING"�����ͱȽϣ����������ӣ��Ϳ���ʵ��������ʱ�����д��
RewriteEngine on RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700 RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900 RewriteRule ^foo\.html$ foo.day.html RewriteRule ^foo\.html$ foo.night.html
����ʹURLfoo.html��07:00-19:00ʱָ��foo.day.html ����������ʱ�䣬��ָ��foo.night.html ������ҳ��һ������Ĺ���...
��ת���˴���.html�ļ�Ϊ.phtml ��ʹ�ĵ�.YYYY���ɳ�Ϊ�ĵ�.XXXX����α���URL����ǰ����(��Ȼ����ش���)��
ֻ�밴���ļ�����д�������Դ����µ���չ�����ļ��Ƿ���ڣ�������ڣ������µģ�������Ȼ��ԭ���ġ�
# 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$ $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�����о������ļ��������ˡ�
ͨ�����¹����ڲ�����д�ϵ�URLΪ�µģ�
RewriteEngine on RewriteBase /~quux/ RewriteRule ^foo\.html$ bar.html
��Ȼ�ٶ��Ѿ����ļ�bar.html
����Ϊfoo.html
����Ҫ���ϵ�URL��ǰ���ݣ�����Ҫ���û��õ��ļ��������İ�ʾ����������ĵ�ַ������ʾ�����µ�URL��
��һ��HTTP��ǿ���ض����Ըı���������û������ϵ���ʾ��
RewriteEngine on RewriteBase /~quux/ RewriteRule ^foo\.html$ bar.html [R]
���ٶ���Ҫ�Ķ���ҳ�棬��ʱ���б�Ҫ�ṩ���������������ѵ����ݣ��������µ�Netscape�ṩ��İ汾����Lynx�ṩ��С���İ汾��������������������ṩһ������һ��İ汾��
�Դˣ�����Э������Ϊ������Ϊ��������ṩ������ʽ�����ͣ�����ֻ����HTTPͷ"User-Agent"����취�����¹��������������������HTTPͷ"User-Agent"��"Mozilla/3"��ͷ����ҳ��foo.html
����дΪfoo.NS.html
��������д������ֹ�������"Lynx"���߰汾��Ϊ1��2��"Mozilla"������дΪfoo.20.html
�����������е�������յ���ҳ������foo.32.html
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]
�ٶ�����Ҫ�����ǵ����ƿռ����������Զ��������ҳ�档��FTP��������������mirror
�������ڱ��ػ�����ά��һ����Զ�����ݵ����µĿ�������web�����������������Ƶ�����HTTP��webcopy
���������ּ�������һ����Ҫ��ȱ�㣺�˱��ؿ�������ͨ����������ִ�������¡����ԣ��ȽϺõķ����ǣ������þ�̬�������ö�̬����������������ʱ�Զ�����(Զ�������ϸ��µ�����)��
Ϊ�ˣ�ʹ�ô�������(Proxy Throughput)����(flag [P]
)����ӳ��Զ��ҳ����������Զ�������������ǵ����ƿռ䣺
RewriteEngine on RewriteBase /~quux/ RewriteRule ^hotsheet/(.*)$ http://www.tstimpreso.com/hotsheet/$1 [P]
RewriteEngine on RewriteBase /~quux/ RewriteRule ^usa-news\.html$ 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
����һ�����ܷ���ǽ������(�ڲ�)Intranet(www2.quux-corp.dom
)�ϱ����ά��ʵ�����ݣ��������������ҵ��(�ⲿ)Internet web������(www.quux-corp.dom
)������ķ��������ַ������ⲿ�������ڿ���ʱ����ڲ�������ȡ�ñ���������ݡ�
���ȣ�����ȷ������ǽ���ڲ��������ı�������ֻ������ⲿ������ȡ�����ݡ�������(packet-filtering)����ǽ�����������ƶ�����ǽ����
ALLOW Host www.quux-corp.dom Port >1024 --> Host www2.quux-corp.dom Port 80 DENY Host * Port * --> Host www2.quux-corp.dom Port 80
�����ʵ�����ã�ֻҪ�����������������ɡ����ţ�����ͨ�������̨��ȡ��ʧ���ݵ�mod_rewrite
����
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.foo.com
�ĸ��ص�www[0-5].foo.com
(һ����6��������)?
��������������ܵĽ���������ڴˣ���������ͨ��Ϊ������DNS���ķ������������ʹ��mod_rewrite
�ķ���:
��ķ�������BIND
��DNSѭ�����ԣ�ֻҪ����������www[0-9].foo.com
��DNS��A(��ַ)��¼���磺
www0 IN A 1.2.3.1 www1 IN A 1.2.3.2 www2 IN A 1.2.3.3 www3 IN A 1.2.3.4 www4 IN A 1.2.3.5 www5 IN A 1.2.3.6
Ȼ���������¸���:
www IN CNAME www0.foo.com. IN CNAME www1.foo.com. IN CNAME www2.foo.com. IN CNAME www3.foo.com. IN CNAME www4.foo.com. IN CNAME www5.foo.com. IN CNAME www6.foo.com.
ע�⣬�����������ƺ��Ǵ���ģ�����ʵ�ϣ�����ȷ��BIND
�е�һ��Ԥ�ڵ����ԣ�����Ҳ���������á�������Σ�����www.foo.com
�Ѿ���������BIND
���Ը���www0-www6
����Ȼÿ���ڴ����ϻ��������û�/ѭ�����ͻ��˵�������Ա���ɢ�����������������Ⲣ����һ������ĸ��ؾ��ⷽ������ΪDNS������Ϣ���Ա��������������Ʒ��������壬��һ��www.foo.com
������ΪwwwN.foo.com
������������������www.foo.com
���������ս������ȷ�ģ���Ϊ�����������ȷ����ɢ��������������
һ�ֳ���Ļ���DNS�ĸ��ؾ��ⷽ����ʹ��http://www.stanford.edu/~schemers/docs/lbnamed/lbnamed.html��lbnamed
��������һ��Perl5���������ɸ������ߣ�ʵ���������Ļ���DNS�ĸ��ؾ��⡣
����һ��ʹ��mod_rewrite
��������������Եķ��������ȣ���DNS��¼�н�www0.foo.com
�̶�Ϊwww.foo.com
�����£�
www IN CNAME www0.foo.com.
�����www0.foo.com
ת��Ϊһ��רְ���������������������������е�����URLͨ���ڲ������ɢ������5��������(www1-www5
)��Ϊ�ˣ����뽨��һ������������URL����һ�����ؾ���ű�lb.pl
��
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/$_"; } ##EOF##
www0.foo.com
�ƺ�Ҳ�ᳬ��ѽ�����ǣ�û������ȷ�ᳬ�أ����������صĽ����ǼĴ�������������������SSI��CGI��ePerl�ȵȵĴ�����ȫ��������������ɵģ��������Ҫ�㡣����һ��Ӳ�����������Cisco��һ����LocalDirector�Ķ�����ʵ����TCP/IP��ĸ��ؾ��⣬��ʵ�ϣ�����һ��λ����վ��Ⱥǰ�˵ĵ�·�����ء���������㹻�ʽ���ҵ�ȷ��Ҫ�����ܵĽ����������ô�����������
## ## apache-rproxy.conf -- Apache configuration for Reverse Proxy Usage ## # server type ServerType standalone Listen 8000 MinSpareServers 16 StartServers 16 MaxSpareServers 16 MaxClients 16 MaxRequestsPerChild 100 # server operation parameters KeepAlive on MaxKeepAliveRequests 100 KeepAliveTimeout 15 Timeout 400 IdentityCheck off HostnameLookups off # paths to runtime files PidFile /path/to/apache-rproxy.pid LockFile /path/to/apache-rproxy.lock ErrorLog /path/to/apache-rproxy.elog CustomLog /path/to/apache-rproxy.dlog "%{%v/%T}t %h -> %{SERVER}e URL: %U" # unused paths ServerRoot /tmp DocumentRoot /tmp CacheRoot /tmp RewriteLog /dev/null TransferLog /dev/null TypesConfig /dev/null AccessConfig /dev/null ResourceConfig /dev/null # speed up and secure processing <Directory /> Options -FollowSymLinks -SymLinksIfOwnerMatch AllowOverride None </Directory> # the status page for monitoring the reverse proxy <Location /apache-rproxy-status> SetHandler server-status </Location> # enable the URL rewriting engine RewriteEngine on RewriteLogLevel 0 # define a rewriting map with value-lists where # mod_rewrite randomly chooses a particular value RewriteMap server rnd:/path/to/apache-rproxy.conf-servers # make sure the status page is handled locally # and make sure no one uses our proxy except ourself RewriteRule ^/apache-rproxy-status.* - [L] RewriteRule ^(http|ftp)://.* - [F] # now choose the possible servers for particular URL types RewriteRule ^/(.*\.(cgi|shtml))$ to://${server:dynamic}/$1 [S=1] RewriteRule ^/(.*)$ to://${server:static}/$1 # and delegate the generated URL by passing it # through the proxy module RewriteRule ^to://([^/]+)/(.*) http://$1/$2 [E=SERVER:$1,P,L] # and make really sure all other stuff is forbidden # when it should survive the above rules... RewriteRule .* - [F] # enable the Proxy module without caching ProxyRequests on NoCache * # setup URL reverse mapping for redirect reponses ProxyPassReverse / http://www1.foo.dom/ ProxyPassReverse / http://www2.foo.dom/ ProxyPassReverse / http://www3.foo.dom/ ProxyPassReverse / http://www4.foo.dom/ ProxyPassReverse / http://www5.foo.dom/ ProxyPassReverse / http://www6.foo.dom/
## ## apache-rproxy.conf-servers -- Apache/mod_rewrite selection table ## # list of backend servers which serve static # pages (HTML files and Images, etc.) static www1.foo.dom|www2.foo.dom|www3.foo.dom|www4.foo.dom # list of backend servers which serve dynamically # generated page (CGI programs or mod_perl scripts) dynamic www5.foo.dom|www6.foo.dom
���������������CGI�������÷���ɬ�����������֮���á���ʹ��Apache��MEME���͵Ķ�����������Ҳ������CGI������Ҫ���������а�������URL(PATH_INFO
��QUERY_STRINGS
)ʱ�źܺ��á����ȣ�����һ���µĺ�Ϊ.scgi
(��ȫCGI)�ļ����ͣ��䴦�����Ǻܳ�����cgiwrap
���������ǣ����ʹ��ͬ��URL�滮(������)�����û�����Ŀ¼�е�һ���ļ���URL��/u/user/foo/bar.scgi
������cgiwrap
Ҫ���URL�ĸ�ʽ��/~user/foo/bar.scgi/
�����¹�������������⣺
RewriteRule ^/[uge]/([^/]+)/\.www/(.+)\.scgi(.*) ... ... /internal/cgi/user/cgiwrap/~$1/$2.scgi$3 [NS,T=application/x-http-cgi]
���⣬������Ҫʹ����������wwwlog
(��ʾaccess.log
�е�һ��URL����)��wwwidx
(��һ��URL��������Glimpse)����������Щ�����ṩURL������Ϊ����������磬��/u/user/foo/
ִ��swwidx
����ij����������ģ�
/internal/cgi/user/swwidx?i=/u/user/foo/
��ȱ���ǣ�����ͬʱӲ���볬���е������CGI��·����������������������Ҫ���Ѵ���ʱ�����ĸ���������
��������һ��������µ�URL��ʽ���Զ�ƴװCGI������
RewriteRule ^/([uge])/([^/]+)(/?.*)/\* /internal/cgi/user/wwwidx?i=/$1/$2$3/ RewriteRule ^/([uge])/([^/]+)(/?.*):log /internal/cgi/user/wwwlog?f=/$1/$2$3
���ڣ����������/u/user/foo/
�ij������ˣ�
HREF="*"
���ᱻ�ڲ����Զ�ת��Ϊ
/internal/cgi/user/wwwidx?i=/u/user/foo/
��ˣ�����Ϊʹ��":log
"�ij�����ƴװ������CGI����IJ�����
�����ת����̬ҳ��foo.html
Ϊ��̬��foo.cgi
������Ϊ�����/�û��������
ֻ����д��URLΪCGI-script ����ǿ��Ϊ������ΪCGI-script���е���ȷ��MIME���͡���ˣ���/~quux/foo.html
��������ʵ��ִ��/~quux/foo.cgi
��
RewriteEngine on RewriteBase /~quux/ RewriteRule ^foo\.html$ foo.cgi [T=application/x-httpd-cgi]
����һ�����ѽ�Ĺ��ܣ���̬���ɵľ�̬ҳ�棬����Ӧ����Ϊ��̬ҳ�淢��(���ļ�ϵͳ�ж�����Ȼ��ֱ�ӷ���ȥ)�������������ʧ�ˣ����ɷ�������̬���ɡ����������Ծ�̬���ṩCGI���ɵ�ҳ�棬��������(������һ��cronjob)ɾ������Щ��̬ҳ�棬���������ݿ��Եõ����¡�
RewriteCond %{REQUEST_FILENAME} !-s RewriteRule ^page\.html$ page.cgi [T=application/x-httpd-cgi,L]
���������page.html
�����ڻ����ļ���СΪnull �����page.html
������ᵼ��page.cgi
���������������page.cgi
��һ�������д��page.html
��(ͬʱҲд��STDOUT
)�ij����CGI�ű���ִ����ϣ���������page.html
�����ݷ��������������Ҫǿ�Ƹ��������ݣ�ֻ��ɾ��page.html
����(ͨ����һ��cronjob���)��
����һ�����ӵ�ҳ�棬�ܹ����ñ༭��д��һ�����µİ汾ʱ�Զ���������ϵõ�ˢ�£��ⲻ�Ǻܺ����������
���ǿ��е�! ����Ҫ�ۺ�����MIME��ɷ֡�web��������NPH��mod_rewrite
��URL�ٿ����ԡ����ȣ�����һ���µ�URL���ԣ������ļ�ϵͳ�и���ʱ��Ҫˢ�µ�����URL����":refresh
" ��
RewriteRule ^(/[uge]/[^/]+/?.*):refresh /internal/cgi/apache/nph-refresh?f=$1
Ȼ����URL
/u/foo/bar/page.html:refresh
���ڲ��زٿش�URL
/internal/cgi/apache/nph-refresh?f=/u/foo/bar/page.html
���ž���NPH-CGI�ű������ˡ���Ȼ�����dz�˵"������Ϊһ����ϰ��������"�����һ��Ǹ������ˡ�
#!/sw/bin/perl ## ## nph-refresh -- NPH/CGI script for auto refreshing pages ## Copyright (c) 1997 Ralf S. Engelschall, All Rights Reserved. ## $| = 1; # split the QUERY_STRING variable @pairs = split(/&/, $ENV{'QUERY_STRING'}); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $name =~ tr/A-Z/a-z/; $name = 'QS_' . $name; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; eval "\$$name = \"$value\""; } $QS_s = 1 if ($QS_s eq ''); $QS_n = 3600 if ($QS_n eq ''); if ($QS_f eq '') { print "HTTP/1.0 200 OK\n"; print "Content-type: text/html\n\n"; print "<b>ERROR</b>: No file given\n"; exit(0); } if (! -f $QS_f) { print "HTTP/1.0 200 OK\n"; print "Content-type: text/html\n\n"; print "<b>ERROR</b>: File $QS_f not found\n"; exit(0); } sub print_http_headers_multipart_begin { print "HTTP/1.0 200 OK\n"; $bound = "ThisRandomString12345"; print "Content-type: multipart/x-mixed-replace;boundary=$bound\n"; &print_http_headers_multipart_next; } sub print_http_headers_multipart_next { print "\n--$bound\n"; } sub print_http_headers_multipart_end { print "\n--$bound--\n"; } sub displayhtml { local($buffer) = @_; $len = length($buffer); print "Content-type: text/html\n"; print "Content-length: $len\n\n"; print $buffer; } sub readfile { local($file) = @_; local(*FP, $size, $buffer, $bytes); ($x, $x, $x, $x, $x, $x, $x, $size) = stat($file); $size = sprintf("%d", $size); open(FP, "<$file"); $bytes = sysread(FP, $buffer, $size); close(FP); return $buffer; } $buffer = &readfile($QS_f); &print_http_headers_multipart_begin; &displayhtml($buffer); sub mystat { local($file) = $_[0]; local($time); ($x, $x, $x, $x, $x, $x, $x, $x, $x, $mtime) = stat($file); return $mtime; } $mtimeL = &mystat($QS_f); $mtime = $mtime; for ($n = 0; $n < $QS_n; $n++) { while (1) { $mtime = &mystat($QS_f); if ($mtime ne $mtimeL) { $mtimeL = $mtime; sleep(2); $buffer = &readfile($QS_f); &print_http_headers_multipart_next; &displayhtml($buffer); sleep(5); $mtimeL = &mystat($QS_f); last; } sleep($QS_s); } } &print_http_headers_multipart_end; exit(0); ##EOF##
Apache��<VirtualHost>
���ܺ�ǿ�����м�ʮ��������������������еúܺã������������ISP����Ҫ�ṩ���ٸ�������������ô��Ͳ���һ����ѵ�ѡ���ˡ�
Ϊ�ˣ���Ҫ����������(Proxy Throughput)����(flag [P]
)ӳ��Զ��ҳ����������Զ�����������Լ������ƿռ䣺
## ## vhost.map ## www.vhost1.dom:80 /path/to/docroot/vhost1 www.vhost2.dom:80 /path/to/docroot/vhost2 : www.vhostN.dom:80 /path/to/docroot/vhostN
## ## httpd.conf ## : # use the canonical hostname on redirects, etc. UseCanonicalName on : # add the virtual host in front of the CLF-format CustomLog /path/to/access_log "%{VHOST}e %h %l %u %t \"%r\" %>s %b" : # enable the rewriting engine in the main server RewriteEngine on # define two maps: one for fixing the URL and one which defines # the available virtual hosts with their corresponding # DocumentRoot. RewriteMap lowercase int:tolower RewriteMap vhost txt:/path/to/vhost.map # Now do the actual virtual host mapping # via a huge and complicated single rule: # # 1. make sure we don't map for common locations RewriteCond %{REQUEST_URL} !^/commonurl1/.* RewriteCond %{REQUEST_URL} !^/commonurl2/.* : RewriteCond %{REQUEST_URL} !^/commonurlN/.* # # 2. make sure we have a Host header, because # currently our approach only supports # virtual hosting through this header RewriteCond %{HTTP_HOST} !^$ # # 3. lowercase the hostname RewriteCond ${lowercase:%{HTTP_HOST}|NONE} ^(.+)$ # # 4. lookup this hostname in vhost.map and # remember it only when it is a path # (and not "NONE" from above) RewriteCond ${vhost:%1} ^(/.*)$ # # 5. finally we can map the URL to its docroot location # and remember the virtual host for logging puposes RewriteRule ^/(.*)$ %1/$1 [E=VHOST:${lowercase:%{HTTP_HOST}}] :
�����ֹһ����ȫ������robotȡ���ض����������ҳ�棿һ��/robots.txt
�ļ�����������"robot�ų�Э��"���У�����������ֹ����robot��
������һ�������Ծܾ�����������/~quux/foo/arc/
(��һ�������Ŀ¼��������б���ܻ�ʹ�����������ܴ�ĸ���)�ķ��ʡ�������ȷ������ֹ�ض���robot������˵��������ֹrobot���������Dz����ģ�������ͬʱҲ��ֹ���û����ʸ�������Ϊ�ˣ�����Ҫ��HTTPͷ��User-Agent��Ϣ��ƥ�䡣
RewriteCond %{HTTP_USER_AGENT} ^NameOfBadRobot.* RewriteCond %{REMOTE_ADDR} ^123\.45\.67\.[8-9]$ RewriteRule ^/~quux/foo/arc/.+ - [F]
���裬http://www.quux-corp.de/~quux/
��һЩ��ǶͼƬ��ҳ�棬��ЩͼƬ�ܺã����Ծ������ó������������Լ���ҳ�����ˡ���������ͽȻ���������ǵķ���������������ˣ����Dz�Ը���������鷢����
��Ȼ�����Dz���100%�ر�����ЩͼƬ����д����˵�ҳ�棬�����ٿ��ԶԷ���HTTP Refererͷ��������������ơ�
RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://www.quux-corp.de/~quux/.*$ [NC] RewriteRule .*\.gif$ - [F]
RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !.*/foo-with-gif\.html$ RewriteRule ^inlined-in-foo\.gif$ - [F]
��ξܾ�һ���ⲿ�б��е����������Ƿ�������ʹ�ã�
RewriteEngine on RewriteMap hosts-deny txt:/path/to/hosts.deny RewriteCond ${hosts-deny:%{REMOTE_HOST}|NOT-FOUND} !=NOT-FOUND [OR] RewriteCond ${hosts-deny:%{REMOTE_ADDR}|NOT-FOUND} !=NOT-FOUND RewriteRule ^/.* - [F]
��ξܾ�ij���������������ض��������û�ʹ��Apache����
���ȣ�Ҫȷ��Apache web�������ڱ���ʱ�����ļ���mod_rewrite
��mod_proxy
�����棡ʹ����mod_proxy
֮ǰ�����á�Ȼ�����¾ܾ�ij������...
RewriteCond %{REMOTE_HOST} ^badhost\.mydomain\.com$ RewriteRule !^http://[^/.]\.mydomain.com.* - [F]
...���¾ܾ�user@host-dependent:
RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} ^badguy@badhost\.mydomain\.com$ RewriteRule !^http://[^/.]\.mydomain.com.* - [F]
��ʱ����Ҫһ�ַdz��������֤������һ����ȷָ�����û�����������ʣ���û��(��ʹ��mod_authz_host
�Ļ�����֤����ʱ���ܻ���ֵ�)�κ���ʾ��
����ʹ��һ����д�����б����ų����е����ѣ�
RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} !^friend1@client1.quux-corp\.com$ RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} !^friend2@client2.quux-corp\.com$ RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} !^friend3@client3.quux-corp\.com$ RewriteRule ^/~quux/only-for-friends/ - [F]
�������һ������HTTPͷ"Referer"�ķ������Է��䵽�����������ύҳ��?
ʹ�����������Ĺ���...
RewriteMap deflector txt:/path/to/deflector.map RewriteCond %{HTTP_REFERER} !="" RewriteCond ${deflector:%{HTTP_REFERER}} ^-$ RewriteRule ^.* %{HTTP_REFERER} [R,L] RewriteCond %{HTTP_REFERER} !="" RewriteCond ${deflector:%{HTTP_REFERER}|NOT-FOUND} !=NOT-FOUND RewriteRule ^.* ${deflector:%{HTTP_REFERER}} [R,L]
... ����϶�Ӧ����дӳ���ͼ:
## ## deflector.map ## http://www.badguys.com/bad/index.html - http://www.badguys.com/bad/index2.html - http://www.badguys.com/bad/index3.html http://somewhere.com/
�������Զ�������(��ӳ���ͼ��ָ����"-
"ֵ��ʱ��)��������ύҳ�棬����(��ӳ���ͼ��URL�еڶ�������ʱ)���䵽һ���ض���URL��
һ����������������ν���ƺ�����mod_rewrite
�����FOO/BAR/QUUX/֮������⣿
����ʹ��һ����RewriteMap
������ͬ���ⲿRewriteMap
����һ������Apache����ʱ��ִ�У����STDIN
���ձ������URL �����������(ͨ������д����)��URL(����ͬ˳��)��STDOUT
�����
RewriteEngine on RewriteMap quux-map prg:/path/to/map.quux.pl RewriteRule ^/~quux/(.*)$ /~quux/${quux-map:$1}
#!/path/to/perl # disable buffered I/O which would lead # to deadloops for the Apache server $| = 1; # read URLs one per line from stdin and # generate substitution URL on stdout while (<>) { s|^foo/|bar/|; print $_; }
����һ������ʾ�����ӣ�ֻ�ǰ����е�URL /~quux/foo/...
��дΪ /~quux/bar/...
������ʵ�ϣ����������Ի���κ�����Ҫ�Ĺ��ܡ�����Ҫע�⣬��Ȼһ���û�������ʹ��������ֻ��ϵͳ����Ա�ſ������������ĵ�ͼ��