2007-3-15 15:58:00
Apache URL重定向指南

mod_rew入门teè?

Apache mod_rewrite模块是ò?′àURL而又极为复杂的模?£êómod_rewrite你可处à?óoURL有关的问题,你所付出的就是花时?èá?mod_rewrite的复杂架构,一般初学者都很难êêà?mod_rewrite的用·£óêApache×?òòmod_rewriteà·?Apache的D1??

换句话说,当你31êómod_rew做到你期做到你期望的东西,就不要试í??′mod_rew了,因为á£ò?mod_rew的功能实的功能实在过于强大。本章的例子会介绍几个成功的例子给你摸?£2像式般把形式般把你的问ì?′?

实用??··

这里还有很多未被发掘的解决方法,请大家耐心地学习èoêómod_rewrite?

×ò: 由于各人的服务器的配置都有所不同,你可能要更改设定来测试以下例子,àèêómod_aliasomod_userdirêò?é[PT],??êó.htaccess来重定向而非主设定文件等,请尽量理解各例子如何运作,不要生吞活剥μ±??

划RL1?

?1URL

:

在某些网页服务器中,一项资源可能?óê?URL,通常都会公2ò?1URL(即真?··μURL)£??URL都会被视为快捷方式或只供内部使用等,无论用户在使用快捷方ê??1URL,用户最后所重??μμURL必需??1?

··:

我们可将所有·?1μURL重定向??1μURL中,以下例子把非?1μ?/~user」换成?1μ?/u/user」,并??é?/」o???.

RewriteRule   ^/~([^/]+)/?(.*)    /u/$1/$2  [R]
RewriteRule   ^/([uge])/([^/]+)$  /$1/$2/   [R]

正规???3

:

(ê?)

··:

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]

Document被移动t±ò?

:

URLμ?/」通常都?ó?μDocumentRooté£μDocumentRoot有时并非重始就限定在某个目录上,它可能只是一个或多个目录的对照而矣。例如我们的内áí??/e/www/ (WWWμ???)o/e/sww/ (内联网μ???)等等,因为所有的网页资á?·?/e/www/目录内,我们要确定所有内嵌的图像都能正è?ê?

··:

我们?ò°?/」重????/e/www/?£ómod_rewrite来??±ómod_alias来解决更为简?£ò?URL±???±?URLμ?2·,但重定向因可能涉及另一台服务器而需要不同的?×2·(前缀2·òêDocumentRoot??)£?òmod_rewrite是最好的??··::

RewriteEngine on
RewriteRule   ^/$  /e/www/  [R]

结尾D??ì

:

每个网主都曾受到结尾斜线问题的折?£è?URL中没有结尾斜线,服务器??è?URL无效并返回错误,因为服务????/~quux/fooè??foo这个档案,而非显示这个目录。其实很多时候,这问题应留待用户×???/」去解决,但有时你也可以完成步骤。例如你×á?′URL重定向,而目的μ?ò?CGI3D?

··:

最直观的方·?êáApache自??é?/」,使用外部重定向令浏览器能正确找到档案,若我们只做内部重定向,就只能正确显示目录页,在这目录页的图像文件?ò??URL的问题而找不到。例如????/~quux/foo/index.htmlμimage.gif时,重定向o?±3/~quux/image.gif?

所以我们应使用ò?··:

RewriteEngine  on
RewriteBase    /~quux/
RewriteRule    ^foo$  foo/  [R]

这方法òêóó.htac文件在各文件在各目录内设定,但这设定会覆盖原先主配????

RewriteEngine  on
RewriteBase    /~quux/
RewriteCond    %{REQUEST_FILENAME}  -d
RewriteRule    ^(.+[^/])$           $1/  [R]

利ó?òμ面规划版面规划í?è×

:

所有的网页服务器都ó?íμURL版面,即无论用户向哪个主机·3??URL,用户都会接收到相同的íò£êURL独立于服务器本身。我们的目的在óèo?Apache服务器不能响应时,都能有ò?31(而又独立于服???×)的网页传送给用户,设立网络群组可将这网页送??3?

··:

首先,服务器需要一外部文件把网站的用户、用户组及其它资料存储,这文件的?êè?

user1  server_of_user1
user2  server_of_user2
:      :

把以上×á′èmap.xxx-to-host。然后指示·??°定向,重??£ó

/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

当服务器接收到2?èμ,服务时,服务器会跟随以??ê把像到特映像到特?的μ°(若没有相并没有相对应的记录,就会???? serv上r0 é):

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\

把主目录移到新的网ò·??

:

有很多网主都有ò??ì:在升级时把所有用户主目录由旧的服务器移到新的服??é?

··:

êómod_rewrite可以简单地解决这问题£°?ó/~user/anypathURL????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?

··:

按以??á?URL直接对映到档案?í??

RewriteEngine on
RewriteRule   ^/~(([a-z])[a-z0-9]+)(.*)  /home/$2/$1/.www$3

重新组织μ°?í

:

这是一个麻·μà×:在不用更动现有目录结构?£êóRewriteRules来显示整个目录结构?±?£net.sw是ò?×?Unix免费软件的资料夹,并以下列结1′′£

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<SPAN style="mso-spacerx
奇丽浮云 | 阅读全文 | 回复(0) | 引用通告 | 编辑
发表评论:
表点我哦
时间记忆
<<  < 2011 - >  >>
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
您的信息
我的相册
最新日志
最新评论
最新回复
我的好友
站点信息
 
天涯博客欢迎您!