ºìÁªLinuxÃÅ»§
Linux°ïÖú

ApacheÖØд¹æÔòµÄ³£¼ûÓ¦ÓÃ

·¢²¼Ê±¼ä:2006-03-13 00:16:25À´Ô´:ºìÁª×÷Õß:ÐÂÐÂ
---- ±¾ÎÄÖ¼ÔÚÌṩÈçºÎÓÃApacheÖØд¹æÔòÀ´½â¾öһЩ³£¼ûµÄURLÖØд·½·¨µÄÎÊÌ⣬ͨ¹ý³£¼ûµÄʵÀý¸øÓû§Ò»Ð©Ê¹ÓÃÖØд¹æÔòµÄ»ù±¾·½·¨ºÍÏßË÷¡£

Ò»¡¢ÎªÊ²Ã´ÐèÒªÓÃÖØд¹æÔò

---- ÍøÕ¾µÄÉúÃüÔÚÓÚ²»¶ÏµØ½øÐиüкÍά»¤£¬¸ù¾ÝÒµÎñ·¢Õ¹µÄÐèÇóתÒÆ·þÎñÆ÷½øÐÐά»¤¡¢ÖØÐÂ×é֯Ŀ¼½á¹¹¡¢±ä»»URLÉõÖÁ¸Ä±äµ½ÐµÄÓòÃûµÈÇé¿öÊǾ­³£·¢ÉúµÄ¡£ÎªÁËÈÿͻ§²»»áÒò´ËÊܵ½ÈκÎÓ°Ï죬×îºÃµÄ·½·¨¾ÍÊÇʹÓÃApache Rewrite Rule(ÖØд¹æÔò)¡£

¶þ¡¢ÖØд¹æÔòµÄ×÷Ó÷¶Î§

---- 1£®Ê¹ÓÃÔÚApacheÖ÷ÅäÖÃÎļþhttpd.confÖС£
---- 2£®Ê¹ÓÃÔÚhttpd.confÀﶨÒåµÄÐéÄâÖ÷»úÅäÖÃÖС£
---- 3£®Ê¹ÓÃÔÚ»ù±¾Ä¿Â¼µÄ¿çÔ½ÅäÖÃÎļþ.htaccessÖС£

Èý¡¢ÖØд¹æÔòµÄÓ¦ÓÃÌõ¼þ

---- µ±Óû§µÄWebÇëÇó×îÖÕ±»µ¼Ïòµ½Ä³Ì¨Web·þÎñÆ÷µÄApacheÊØ»¤½ø³Ì£¬Apache¸ù¾ÝÅäÖÃÎļþÅжϸÃÇëÇóÊÇÖ÷ÅäÖû¹ÊÇÐéÄâÖ÷»ú£¬ÔÙ¸ù¾ÝÓû§ÔÚä¯ÀÀÆ÷ÖÐÇëÇóµÄURLÀ´Æ¥ÅäÖØд¹æÔò£¬²¢ÇÒ¸ù¾Ýʵ¼ÊµÄÇëÇ󷾶ƥÅä.htaccessÖеÄÖØд¹æÔò£¬×îºó°ÑÇëÇóµÄÄÚÈÝ´«»Ø¸øÓû§¡£¸ÃÏìÓ¦¿ÉÄÜÓÐ2ÖÖ¡£

---- 1£®½«ÇëÇóÄÚÈÝÍⲿÖض¨Ïò(Redirect)µ½ÁíÒ»¸öURL
---- ÈÃä¯ÀÀÆ÷ÔÙ´ÎÒÔеÄURL·¢³öÇëÇó(R=301»òÕßR=302£¬ÁÙʱµÄ»òÊÇÓÀ¾ÃµÄÖض¨Ïò)¡£

---- ÀýÈ磬һ¸öÍøÕ¾ÓÐÕý¹æµÄURLºÍ±ðÃûURL£¬¶Ô±ðÃûURL½øÐÐÖض¨Ïòµ½Õý¹æURL£¬»òÕßÍøÕ¾¸Ä»»³ÉÁËеÄÓòÃû£¬Ôò°Ñ¾ÉµÄÓòÃûÖض¨Ïòµ½ÐµÄÓòÃû¡£

---- 2£®ÓÉApacheÄÚ²¿×ÓÇëÇó´úÀí²úÉúеÄÄÚÈÝËͻظø¿Í»§[P,L]
---- ÕâÊÇApacheÄÚ²¿¸ù¾ÝÖØдºóµÄURL£¬Í¨¹ý´úÀíÄ£¿éÇëÇóÄÚÈݲ¢½«×îÖÕÄÚÈÝËͻظø¿Í»§£¬¿Í»§¶Ëä¯ÀÀÆ÷²»±ØÔÙ´ÎÇëÇó£¬ä¯ÀÀÆ÷ÖеÄURL²»»á±»ÖØд£¬µ«Êµ¼ÊÄÚÈÝÓÉApache¸ù¾ÝÖØд¹æÔòºóµÄURLÉú³É¡£

---- ÀýÈ磬ÔÚ¹«Ë¾·À»ðǽÉÏÔËÐеÄApacheÆô¶¯ÕâÖÖ´úÀíÖØд¹æÔò£¬´úÀí¶ÔÄÚ²¿Íø¶ÎÉϵÄWeb·þÎñÆ÷µÄÇëÇó¡£

ËÄ¡¢ÖØд¹æÔòÔõÑù¹¤×÷

---- ÎÒÃǼٶ¨ÔÚ±àÒëApacheʱÒѾ­°Ñmod_rewrite±àÒë³ÉÄ£¿é£¬È·ÐÅÄúµÄhttpd.confÖÐÓÐLoadModule rewrite_module libexec/mod_rewrite.so£¬²¢ÇÒÔÚAddmoduleÖÐÓÐAddmodule mod_rewrite.c£¬Ôò¿ÉÒÔʹÓÃÖØд¹æÔò¡£

---- µ±ÍⲿÇëÇóµ½´ïApache£¬Apacheµ÷ÓÃÖØд¹æÔòÖеĶ¨ÒåÀ´ÖØдÓÉÓû§ä¯ÀÀÆ÷Ö¸¶¨ÇëÇóµÄURL£¬×îºó±»ÖØдµÄURLÈç¹ûÊÇÖض¨Ïò£¬ÔòËͽ»ä¯ÀÀÆ÷×öÔÙÒ»´ÎÇëÇó£»Èç¹ûÊÇ´úÀíÔò°ÑÖØдºóµÄURL½»¸ø´úÀíÄ£¿éÇëÇó×îÖÕµÄÄÚÈÝ(Content)£¬×îºó°ÑÄÚÈÝËͻظøä¯ÀÀÆ÷¡£

Îå¡¢ºÎʱʹÓÃ.htaccessÖеÄÖØд¹æÔò¶¨Òå

---- ¼ÙÈçÄú¶ÔÍøÕ¾ÄÚÈÝËùÔڵķþÎñÆ÷ûÓйÜÀíԱȨÏÞ£¬»òÕßÄúµÄÍøÕ¾ÄÚÈÝ·ÅÔÚISPµÄ·þÎñÆ÷ÉÏÍйܣ¬ÎÞ·¨¸ÄдÖ÷ÅäÖÃÎļþ£¬µ«ÊÇÄú¶ÔWebÕ¾µãÄÚÈÝËùÔÚµÄĿ¼ÓÐдȨÏÞ£¬Ôò¿ÉÒÔÉèÖÃ×Ô¼ºµÄ.htaccessÎļþ´ïµ½Í¬ÑùµÄÄ¿µÄ¡£µ«ÄúÐèҪȷ¶¨Ö÷ÅäÖÃÎļþÖжÔÄúµÄÍøÕ¾ËùÔÚµÄĿ¼¶¨ÒåÁËÏÂÃæµÄÄÚÈÝ£¬·ñÔòÄúµÄ.htaccess²»»á¹¤×÷¡£

---- < Directory /usr/local/apache/htdocs/www.abc.com> options indexes followsymLinks
---- allowoverride all
---- < /Directory >

Áù¡¢Ó¦ÓþÙÀý

---- ¼Ù¶¨Apache±»±àÒë°²×°ÔÚÖ÷»ú192.168.1.56µÄ/usr/local/apacheĿ¼ÏÂÃ棬ͬʱ±àÒëÁËÖØдºÍ´úÀíÄ£¿é¡£

---- 1£®Òþ²ØApacheϵÄij¸öĿ¼£¬Ê¹µÃ¶Ô¸ÃĿ¼µÄÈκÎÇëÇó¶¼Öض¨Ïòµ½ÁíÒ»¸öÎļþ
---- (1)httpd.confµÄʵÏÖ·½·¨
---- ÎÒÃǽ«ÏÂÃæµÄ²¿·Ö·Åµ½/usr/local/apache/conf/httpd.confÖС£

---- < Directory "/usr/local/apache/htdocs/manual/"> options Indexes followsymlinks
---- allowoverride all
---- rewriteengine on
---- rewritebase /
---- rewriterule ^(.*)$ index.html.en [R=301]
---- < /Directory >

---- ×¢: ¡°rewriteengine on¡±ÎªÖØдÒýÇ濪¹Ø£¬Èç¹ûÉèΪ¡°off¡±£¬ÔòÈκÎÖØд¹æÔò¶¨Ò彫²»±»Ó¦Ó㬸ÿª¹ØµÄÁíÒ»Óô¦¾ÍÊÇÈç¹ûΪÁËÁÙʱȥµôÖØд¹æÔò£¬¿ÉÒÔ½«ÒýÇ濪¹ØÉèΪ¡°off¡±ÔÙÖØÐÂÆô¶¯Apache¼´¿É£¬²»±Ø½«ÆäÖеĸ÷ÌõÖØд¹æÔò×¢Ê͵ô¡£

---- ¡°rewritebase /¡±µÄ×÷ÓÃÊÇÈç¹ûÔÚÏÂÃæµÄrewriterule¶¨ÒåÖб»ÖØдºóµÄ²¿·Ö(´Ë´¦ÎªÎļþÃûindex.html.en)Ç°ÃæûÓС°/¡±£¬Ôò±íÃ÷ÊÇÏà¶ÔĿ¼£¬Ïà¶ÔÓÚÕâ¸örewritebaseºóÃæµÄ¶¨ÒåÒ²¾ÍÊÇ/usr/local/apache/htdocs/index.html.en£¬·ñÔò£¬Èç¹û´Ë´¦Ã»ÓС°rewritebase /¡±ÕâÒ»ÏÔò±»ÖØд³Éhttp://192.168.1.56/usr/local/apache/htdocs/manual/index.html.en£¬ÏÔÈ»ÊDz»ÕýÈ·µÄ¡£

---- ÎÒÃÇÒ²¿ÉÒÔ²»Óá°rewritebase /¡±£¬¶øÊǽ«Æä¸ÄΪÈçϲ¿·Ö¡£
---- rewriteengine on
---- rewriterule ^(.*)$ /index.html.en [R=301]
---- »òÕ߸ü¸ÄΪ£º
---- rewriteengine on
---- rewriterule ^(.*)$ http://192.168.1.56/index.html.en [R=301]

---- (2).htaccessµÄʵÏÖ·½·¨
---- ÎÒÃǽ«ÏÂÃæµÄ²¿·Ö·Åµ½httpd.confÖС£

---- < Directory "/usr/local/apache/htdocs/manual/"> options Indexes followsymlinks
---- allowoverride all
---- < /Directory >

---- È»ºó½«ÏÂÃæµÄ²¿·Ö·Åµ½/usr/local/apache/htdocs/manual/.htaccessÖС£
---- rewriteengine on
---- rewritebase /
---- rewriterule ^(.*)$ index.html.en [R=301]

---- ×¢: ¶ÔÎļþ.htaccessËù×öµÄÈκθĶ¯²»ÐèÒªÖØÆô¶¯Apache¡£

---- Äú»¹¿ÉÒÔÀûÓÃ.htaccess·½°¸½«Õâ¸ömanualĿ¼Öض¨Ïòµ½Óû§jephe×Ô¼ºµÄÖ÷Ŀ¼¡£
---- rewriteengine on
---- rewritebase /~jephe/
---- rewriterule ^(.*)$ $1 [R=301]

---- ÕâÑù£¬¶ÔmanualĿ¼ÏÂÈκÎÎļþµÄÇëÇó±»Öض¨Ïòµ½¡«jepheĿ¼ÏÂÏàͬÎļþµÄÇëÇó¡£

---- 2£®½«http://www.username.domain.com¶ÔÓÚusernameµÄÖ÷Ò³ÇëÇóת»»Îª¶Ôhttp://www.domain.com/usernameµÄÇëÇó
---- ¶ÔÓÚHTTP/1.1µÄÇëÇó°üÀ¨Ò»¸öHost: HTTPÍ·£¬ÎÒÃÇÄÜÓÃÏÂÃæµÄ¹æÔò¼¯ÖØдhttp://www.username.domain .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

---- ×¢: ¡°rewritecond¡±±íÃ÷ÊÇÌõ¼þÖØд¹æÔò£¬µ±Âú×ãºóÃ涨ÒåµÄÌõ¼þºó²Å»áÓ¦ÓÃÏÂÃæµÄÖØд¹æÔò£¬¡°rewritecond¡±Óи÷ÖÖ±äÁ¿£¬Çë²éÔÄÏà¹ØÎĵµ¡£

---- 3£®·À»ðǽÉϵÄÖØд¹æÔò´úÀíÄÚ²¿Íø¶ÎÉÏ·þÎñÆ÷µÄÇëÇó
---- NameVirtualhost 1.2.3.4
---- < Virtualhost 1.2.3.4:80 > servername www.domain.com
---- rewriteengine on
---- proxyrequest on
---- rewriterule ^/(.*)$ http://192.168.1.3/$1 [P,L]
---- < /Virtualhost >

---- ×¢: µ±Íⲿä¯ÀÀÆ÷ÇëÇóhttp://www.domain.comʱ£¬½«±»½âÎöµ½IPµØÖ·1.2.3.4£¬Apache½»ÓÉmod_rewrite´¦Àí£¬×ª»»³Éhttp://192.168.1.3/$1ºóÔÙ½»ÓÉ´úÀíÄ£¿émod_proxy£¬µÃµ½ÄÚÈݺó´«ËÍ»ØÓû§µÄä¯ÀÀÆ÷¡£

---- 4£®»ù±¾Ô¤ÏÈÉ趨µÄת»»Map±í½øÐÐÖØдrewritemap
---- ת»»http://www.domain.com/{countrycode}/anypathµ½Map±íÖй涨µÄURL£¬Ç°ÃæÊÇÐéÄâÖ÷»úÖеĶ¨Òå¡£
---- rewritelog /usr/local/apache/logs/rewrite.log
---- rewriteloglevel 9
---- rewriteengine on
---- proxyrequest on
---- rewritemap sitemap txt:/usr/local/apache/conf/rewrite.map
---- rewriterule ^/([^/] ) /(.*)$ http://%{REMOTE_HOST}::$1 [C]
---- rewriterule (.*)::([a-z] )$ ${sitemap:$2|http://h.i.j.k/} [R=301,L]
---- Îļþ/usr/local/apache/conf/rewrite.mapµÄÄÚÈÝÈçÏÂ:
---- sg http://a.b.c.d/
---- sh http://e.f.g.h/

---- ×¢: µ±Óû§ÇëÇóhttp://www.domain.com/sg/anypathʱ±»ÖØдΪhttp://a.b.c.d/anypath¡£µ±ÐèÒªµ÷ÊÔʱÇëÓÃrewritelogºÍ rewriteloglevel 9ÁªºÏ£¬9Ϊ×î´ó£¬¼´µÃµ½×î¶àµÄµ÷ÊÔÐÅÏ¢£»×îСΪ1£¬±íʾµÃµ½×îÉٵĵ÷ÊÔÐÅÏ¢£»Ä¬ÈÏΪ0£¬±íʾûÓе÷ÊÔÐÅÏ¢¡£

---- sitemapµÄÓï·¨ÊÇ${sitemap: LookupKey | Defaultvalue}£¬ÓÐЩÊéÉÏ°Ñ$д³ÉÁË%ÊÇ´íÎóµÄ¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ