[PHP] URI ÆĶó¸ÞÅÍ Àü´Þ ¹æ¹ý°ú ȯ°æº¯¼ö ºñ±³ - ÀÛ¼ºÀÚ : ±èÄ¥ºÀ - ÀÛ¼ºÀÏ : 2005-09-14: Ãß°¡ 2003-06-27: new build - ³» ¿ë : PHP URI ÆĶó¸ÞÅÍ Àü´Þ ¹æ¹ý°ú ±× »ç¿ë¹ý - ¼ö ÁØ : Ãʺ¸¿ë - Å°¿öµå : PATH_INFO, SCRIPT_NAME, PHP_SELF, PATH_TRANSLATED *ÁÖ) ÀÌ ¹®¼­¿¡ ´ëÇÑ ÃֽŠ³»¿ëÀº ¾Æ·¡ URL¿¡¼­ È®ÀÎÇÒ ¼ö ÀÖ½À´Ï´Ù. http://www.linuxchannel.net/docs/php-parameter.txt --------------------------------------------------------- ¸ñÂ÷ 1. PHP extension¸¦ ÀÌ¿ëÇÑ ¹æ¹ý 2. PHP È®ÀåÀÚ¸¦ ¾ø¾Ö°í »ç¿ëÇÏ´Â ¹æ¹ý 3. »ç¿ë °¡´ÉÇÑ PHP º¯¼ö ºñ±³(Ç¥) 4. ÀÀ¿ë(½ÇÁ¦ Àû¿ë°¡´ÉÇÑ ¹æ¹ý) 4-1. ¾ÆÆÄÄ¡ RedirectMatch ¿¡ ÀÌ¿ë 4-2. ¾ÆÆÄÄ¡ ErrorDocument ¿¡ ÀÌ¿ë 4-3. °¡»óÀÇ ÆÄÀÏÀ̸§ ÀÌ¿ë 5. Èıâ --------------------------------------------------------- ÆÄÀÏ À̸§ÀÌ `foo.php' À̶ó°í °¡Á¤. 1. PHP extension(ÀÌÇÏ 'È®ÀåÀÚ')¸¦ ÀÌ¿ëÇÑ ¹æ¹ý 1) foo.php?id=abc&n=1 2) foo.php/?id=abc&n=1 3) foo.php/id=abc&n=1 4) foo.php/id=abc/n=1 5) foo.php/abc/1 6) foo.php/?id=abc&n=1&/s.ext 7) foo.php/?id=abc&/&n=1 8) foo.php?id=abc&/&n=1 9) foo.php/a/b/?id=abc&n=1 10) ±×¿Ü Á¶ÇÕ °¡´É 1)¹øÀº °¡Àå ¸¹ÀÌ »ç¿ëÇÏ´Â(Ç¥ÁØÀûÀÎ) ¹æ¹ý 2)¹øÀº ƯÀÌÇÏ°Ô 1)¹ø¿¡ '/'°¡ ºÙÀº ¹æ¹ýÀ¸·Î À̰͵µ °¡´ÉÇÔ 3)¹øÀº 2)¹ø¿¡¼­ Äõ¸®(?)¸¦ ¾ø¾Ö´Â ¹æ¹ýÀÌ¸ç ´ë½Å $_GET[id]¿Í °°Àº ¹æ¹ýÀ¸·Î °ªÀ» °¡Á®¿Ã ¼ö ¾øÀ½ 4)¹øÀº 3¹øÀÇ '&'´ë½Å '/'À» ÀÌ¿ëÇÑ ¹æ¹ý 5)¹øÀº °¢°¢ ¼ø¼­´ë·Î ±× °ªÀ» ÇÒ´çÇÏ´Â ¹æ¹ý 6)¹øÀº 2¹ø°ú 4¹øÀ» Á¶ÇÕÇÑ ¹æ¹ýÀ¸·Î Á¦ÀÏ µÚ´Â °¡»óÀÇ ÆÄÀÏÀ̸§ÀÓ 7)¹øÀº 6¹ø°ú °ÅÀÇ °°Áö¸¸ $_GET º¯¼ö¸¦ »ç¿ëÇϱâ À§Çؼ­ º¯ÇüµÈ ÇüÅ 8)¹øÀº 7¹ø°ú °°Àº ÇüÅ·ΠÁ¦ÀÏ ¾ÕÀÇ '/'¸¦ Á¦°ÅÇÑ ÇüÅ 9)¹øÀº 5¹ø°ú 1¹øÀÇ Á¶ÇÕ 2. PHP È®ÀåÀÚ¸¦ ¾ø¾Ö°í »ç¿ëÇÏ´Â ¹æ¹ý 1) foo?id=abc&n=1 2) foo/?id=abc&n=1 3) foo/id=abc&n=1 4) foo/id=abc/n=1 5) foo/abc/1 6) foo/?id=abc&n=1&/s.ext 7) foo/?id=abc&/&n=1 8) foo?id=abc&/&n=1 9) foo/a/b/?id=abc&n=1 10) ±×¿Ü Á¶ÇÕ °¡´É »óÀ§ÀÇ 1¹ø ¹æ¹ý°ú °ÅÀÇ µ¿ÀÏÇÏ¸ç ´Ù¸¸ È®ÀåÀÚ¸¸ ¾øÀ» »ÓÀÓ. (ÀÌ·±½ÄÀ¸·Îµµ °¡´ÉÇϸç, foo µð·ºÅ丮¿Í ¼­·Î ±¸º°µÇ¾î¾ß ÇÔ) *ÁÖ) 2005.09.14 Ãß°¡ ÀÌ °æ¿ì ¾ÆÆÄÄ¡ MultiViews ¿É¼ÇÀÌ ÀÖ¾î¾ß ÇÏ°í, ¶ÇÇÑ mod_negotiation ¸ðµâÀÌ Load µÇ¾î ÀÖ¾î¾ß ÇÕ´Ï´Ù. ¿¹: Options +MultiViews Âü°í: http://linuxchannel.net/docs/manual/content-negotiation.html 3. »ç¿ë °¡´ÉÇÑ PHP º¯¼ö ºñ±³
+----------------------------+----+--------------+------------+------------+
|REQUEST_URI                 |id  |PHP_SELF      |PATH_INFO   |SCRIPT_NAME |
+----------------------------+----+--------------+------------+------------|
|/foo.php?id=abc&n=1         |abc |/foo.php      |NA          |/foo.php    |
+----------------------------+----+--------------+------------+------------|
|/foo.php/?id=abc&n=1        |abc |/foo.php/     |/           |/foo.php    |
+----------------------------+----+--------------+------------+------------|
|/foo.php/id=abc&n=1         |NA  |REQUEST_URI   |/id=abc&n=1 |/foo.php    |
+----------------------------+----+--------------+------------+------------|
|/foo.php/id=abc/n=1         |NA  |REQUEST_URI   |/id=abc/n=1 |/foo.php    |
+----------------------------+----+--------------+------------+------------|
|/foo.php/abc/1              |NA  |REQUEST_URI   |/abc/1      |/foo.php    |
+----------------------------+----+--------------+------------+------------|
|/foo.php/?id=abc&n=1&/s.ext |abc |/foo.php/     |/           |/foo.php    |
+----------------------------+----+--------------+------------+------------|
|/foo.php/?id=abc&/&n=1      |abc |/foo.php/     |/           |/foo.php    |
+----------------------------+----+--------------+------------+------------|
|/foo.php?id=abc&/&n=1       |abc |/foo.php      |NA          |/foo.php    |
+----------------------------+----+--------------+------------+------------|
|/foo.php/a/b/?id=abc&n=1    |abc |/foo.php/a/b/ |/a/b/       |/foo.php    |
+----------------------------+----+--------------+------------+------------+

+----------------------------+----+--------------+------------+------------|
|REQUEST_URI                 |id  |PHP_SELF      |PATH_INFO   |SCRIPT_NAME |
+----------------------------+----+--------------+------------+------------|
|/foo?id=abc&n=1             |abc |/foo          |NA          |/foo        |
+----------------------------+----+--------------+------------+------------|
|/foo/?id=abc&n=1            |abc |/foo/         |/           |/foo        |
+----------------------------+----+--------------+------------+------------|
|/foo/id=abc&n=1             |NA  |REQUEST_URI   |/id=abc&n=1 |/foo        |
+----------------------------+----+--------------+------------+------------|
|/foo/id=abc/n=1             |NA  |REQUEST_URI   |/id=abc/n=1 |/foo        |
+----------------------------+----+--------------+------------+------------|
|/foo/abc/1                  |NA  |REQUEST_URI   |/abc/1      |/foo        |
+----------------------------+----+--------------+------------+------------|
|/foo/?id=abc&n=1&/s.ext     |abc |/foo/         |/           |/foo        |
+----------------------------+----+--------------+------------+------------|
|/foo/?id=abc&/&n=1          |abc |/foo/         |/           |/foo        |
+----------------------------+----+--------------+------------+------------|
|/foo?id=abc&/&n=1           |abc |/foo          |NA          |/foo        |
+----------------------------+----+--------------+------------+------------|
|/foo/a/b/?id=abc&n=1        |abc |/foo/a/b/     |/a/b/       |/foo        |
+----------------------------+----+--------------+------------+------------+
(¾Ç... ÀÌ°Í ±×¸®´Âµ¥ ½Ã°£ÀÌ...T.T)
*Âü°í) - Á¦ÀÏ ±âº»ÀÌ µÇ´Â ȯ°æº¯¼ö´Â DOCUMENT_ROOT ¿Í SCRIPT_FILENAME ÀÓ - DOCUMENT_ROOT = DocumentRoot ÀÇ ¼³Á¤°ª(httpd.conf) - SCRIPT_FILENAME = ½ÇÁ¦ ½ÇÇà ÆÄÀÏÀÇ Àý´ë °æ·Î(Á¦ÀÏ Á¤È®ÇÑ °ª) *°á°ú) - QUERY_STRING Àº REQUEST_URI ¿¡¼­ ù¹ø° '?' ´ÙÀ½ÀÇ ¸ðµç ¹®ÀÚ¿­ - PHP_SELF Àº REQUEST_URI ¿¡¼­ QUERY_STRING ¹®ÀÚ¿­À» Á¦¿ÜÇÑ ³ª¸ÓÁö - PATH_INFO ´Â PHP_SELF ¿¡¼­ µÎ¹ø° '/' ºÎÅÍ ³ª¸ÓÁö ¹®ÀÚ¿­ - SCRIPT_NAME ´Â PHP_SELF ¿¡¼­ PATH_INFO ¸¦ »« ¾ÕºÎºÐ ¹®ÀÚ¿­ (Áï REQUEST_URI ¿¡¼­ '?'¿Í '/' ´ÙÀ½¿¡ ¿À´Â ¸ðµç ¹®ÀÚ¿­À» Á¦¿ÜÇÑ ³ª¸ÓÁö) - PATH_TRANSLATED = PATH_INFO ? DOCUMENT_ROOT + PATH_INFO : SCRIPT_FILENAME (PATH_INFO °ªÀÌ ÀÖ´Ù¸é DOCUMENT_ROOT ¿Í PATH_INFO ÀÇ °áÇÕ, ±× ¿Ü´Â SCRIPT_FILENAME) <ÁÖÀÇ > ¾ÆÆÄÄ¡ Action Áö½ÃÀÚ°¡ Àû¿ëµÉ °æ¿ì ¾à°£ ±î´Ù·Î¿ò. ¼³Á¤¿¹: Action text/plain /bin/action.php ¿äû¿¹: /test.txt?str=string °á°ú(SCRIPT_NAME ÀÌ ±âÁØ) DOCUMENT_ROOT => /www SCRIPT_FILENAME => /www/bin/action.php <-- ÁÖÀÇ SCRIPT_NAME => /bin/action.php <-- Action Áö½ÃÀÚ¿¡¼­ ¼³Á¤°ª QUERY_STRING => str=string PHP_SELF => /bin/action.php/test.txt <-- SCRIPT_NAME ÀÌ ¾Õ¿¡ ºÙÀ½ PATH_INFO => /test.txt <-- PHP_SELF ·Î Âø°¢Çϱ⠽¬¿ò PATH_TRANSLATED => /www/test.txt *ȯ°æº¯¼ö ¾ò´Â ¼ø¼­)
  [DOCUMENT_ROOT]---+
        |           |
  [REQUEST_URI] ----+
        |           |
        |   [SCRIPT_FILENAME]
        |
        |--(? ÀÌÇÏ ÃàÃâ)-----------> [QUERY_STRING]
        |
        `--(QUERY_STRING Á¦°Å)-----> [PHP_SELF]
                                         |
        [PATH_INFO] <---(/ ÀÌÇÏ ÃàÃâ)----+
                                         | 
      [SCRIPT_NAME] <--(PATH_INFO Á¦°Å)--+
4. ÀÀ¿ë(½ÇÁ¦ Àû¿ë°¡´ÉÇÑ ¹æ¹ý) 4-1. ¾ÆÆÄÄ¡ RedirectMatch ¿¡ ÀÌ¿ë RedirectMatch ^/([^./]+)$ /check.php?id=$1 ÀÌ ¼³Á¤Àǵµ´Â /abc ---> /check.php?id=abc (redirect Àǵµ) /ccc ---> /check.php?id=ccc (redirect Àǵµ) /abc/ ---> /abc/ /abc.php ---> /abc.php ÀÌ¿Í °°Àº °á°ú°¡ µÇµµ·Ï ¼³Á¤ÇßÁö¸¸ ¾Ö¼®ÇÏ°Ôµµ Á¦´ë·Î Redirect µÇÁö ¾Ê½À´Ï´Ù. ÀÌÀ¯´Â ¾ÆÆÄÄ¡ÀÇ Redirect ´Â ? ¹®ÀÚ°¡ ÀÎÄÚµù(%3f)µÇ¾î ³Ñ¾î°¡±â ¶§¹®¿¡ PHP ÆÄÀÏ¿¡¼­´Â ¿äûÄõ¸®·Î Çؼ®ÇÏÁö ¾Ê°í ¿ÏÀüÇÑ ÇϳªÀÇ ÆÄÀÏ·Î ÀνÄÇϱ⠶§¹®ÀÔ´Ï´Ù. ½ÇÁ¦·Î ù¹ø° redirect °á°ú´Â /abc ---> /check.php%3fid=abc (redirect Àǵµ ½ÇÆÐ) ÀÌ·¸°Ô ³Ñ¾î°¡¼­ 404 ¿¡·¯¸¦ ³»°Ô µË´Ï´Ù. ÇØ°á¹æ¹ýÀº ?(Äõ¸®)¸¦ »ç¿ëÇÏÁö ¾Ê°í PATH_INFO ȯ°æº¯¼ö°¡ »ý±âµµ·Ï ´ÙÀ½°ú °°Àº ¹æ¹ýÀ» »ç¿ëÇϴ°ÍÀÌ ÁÁ½À´Ï´Ù. ±ÇÀå¼³Á¤) RedirectMatch ^/([^./]+)$ /check.php/id=$1 or RedirectMatch ^/([^./]+)$ /check.php/$1 or RedirectMatch ^/([^./]+)$ /check/id=$1 or RedirectMatch ^/([^./]+)$ /check/$1 4-2. ¾ÆÆÄÄ¡ ErrorDocument ¿¡ ÀÌ¿ë À§¿¡¼­ ±× °³³äÀ» ÀÌÇØÇß´Ù¸é ErrorDocument µµ ÇϳªÀÇ ÆÄÀÏ¿¡¼­ ¸ðµÎ °ü¸®ÇÒ ¼ö ÀÖ½À´Ï´Ù. 1)ÀϹÝÀûÀÎ ¹æ¹ý ErrorDocument 401 /error.php ErrorDocument 403 /error.php ErrorDocument 404 /error.php 2)À̰͵µ °¡´É ErrorDocument 401 /error <-- ½ÇÁ¦·Î´Â error.php ÆÄÀÏ ErrorDocument 403 /error ErrorDocument 404 /error 3)À̰͵µ °¡´É ErrorDocument 401 /error/ <-- error.php ÆÄÀÏ ¶Ç´Â /error/index.php ErrorDocument 403 /error/ ErrorDocument 404 /error/ 4)ÆĶó¸ÞÅÍ·Î Àü´Þ ErrorDocument 401 /error/401 ErrorDocument 403 /error/403 ErrorDocument 404 /error/404 4)¹ø °°Àº °æ¿ì´Â ½ÇÁ¦·Î /error.php/401 /error/index.php/401 ÀÌ µÎ°³ Áß ¾î´À °ÍÀ̵çÁö ¸ÅÄ¡µË´Ï´Ù. Âü°í·Î 4)¹ø ¹æ¹ýÀº ½ÇÁ¦·Î ÀÌ·¸°Ô ¼³Á¤ÇÏÁö ¾Ê¾Æµµ °¡´ÉÇÕ´Ï´Ù. ÀÌÀ¯´Â ErrorDocument ¸¦ ó¸®ÇÏ´Â ÆÄÀÏ¿¡¼­´Â REDIRECT_STATUS ¼­¹öº¯¼ö°¡ Á¸ÀçÇϱ⠶§¹®ÀÔ´Ï´Ù. µû¶ó¼­ ErrorDocument ÄÚµù ¿¹¸¦ µç´Ù¸é, -- /error.php --------------------------- $TITLE[$errcode] ... - ¿äûÇÑ (URI)ÆäÀÌÁö : $request
- ¿¡·¯ ¿ä¾à : $TITLE[$errcode]
- ¿¡·¯ ³»¿ë : $_SERVER[REDIRECT_ERROR_NOTES]; ... _EOF_; ?> ----------------------------------------- ÀÌ·±½ÄÀ¸·Î ÄÚµùÀÌ °¡´ÉÇÕ´Ï´Ù. 4-3. °¡»óÀÇ ÆÄÀÏÀ̸§ ÀÌ¿ë ¿¹1) Request URI : /dowonload/media/foo/bar/setup.exe ÀÌ°ÍÀº ½ÇÁ¦·Î À¥¼­¹ö´Â /dowonload/media/foo/bar/alpha.exe /dowonload.php/media/foo/bar/alpha.exe /dowonload/media.php/foo/bar/alpha.exe /dowonload/media/foo.php/bar/alpha.exe /dowonload/media/foo/bar.php/alpha.exe ÀÌ¿Í °°Àº ÇüÅ·ΠÇؼ®ÀÌ °¡´ÉÇÕ´Ï´Ù. (È®ÀåÀÚ¸¦ ¸ðµÎ ¾ø¾Ö°í ¸µÅ©¸¦ °Å´Â ¹æ¹ý) µû¶ó¼­ ù¹ø° ¹æ¹ýó·³ ½ÇÁ¦·Î À¥¼­¹öÀÇ DocumentRoot ¾ÈÂÊ¿¡ alpha.exe ÆÄÀÏÀÌ ÀÖ´Â °æ¿ì¸¦ Á¦¿ÜÇϸé, ³ª¸ÓÁö ¹æ¹ýÀº ¸ðµÎ DocumentRoot ¹Ù±ùÂÊ¿¡ alpha.exe ÆÄÀÏÀÌ À־ »ó°ü¾ø´Ù´Â ÀǹÌÀÔ´Ï´Ù. ¶ÇÇÑ alpha.exe ÆÄÀÏÀº ½ÇÁ¦ÀÇ ÆÄÀÏÀÏ ¼öµµ ÀÖ°í °¡»óÀÇ ÆÄÀÏÀÏ ¼ö µµ ÀÖ½À´Ï´Ù. µÎ¹ø°ÀÇ °æ¿ì¶ó¸é ½ÇÁ¦·Î download.php ÆÄÀÏ¿¡¼­ PAHT_INFO ÀÇ ¹®ÀÚ¿­À» ÆĽÌÇÏ¿© ÀûÀýÇÏ°Ô Çì´õ¿Í µ¥ÀÌÅ͸¦ Ŭ¶óÀ̾ðÆ®¿¡°Ô Àü¼ÛÇØ¾ß ÇÕ´Ï´Ù. ÀÌ¿Í °°ÀÌ °¡»óÀÇ ÆÄÀÏ À̸§À» ÀÌ¿ëÇÏ´Â °æ¿ì : - È®ÀåÀÚ¿¡ ÀÇÇؼ­ ºê¶ó¿ìÀú(¿¹:MSIE)°¡ Content-type¸¦ ±¸º°ÇÏ´Â °æ¿ì - ´ë¼Ò¹®ÀÚ¸¦ ±¸º°¾øÀÌ »ç¿ë°¡´ÉÇÏ°íÀÚ ÇÒ °æ¿ì - ´Ù¿î·ÎµåÇϱâ Àü¿¡ ¾î¶² ÀÎÁõ ÀýÂ÷°¡ ÇÊ¿äÇÑ °æ¿ì - ½ÇÁ¦ ÆÄÀÏÀÌ DocumentRoot ¹Ù±ùÂÊ¿¡ ÀÖÀ» °æ¿ì - º¸´Ù °£·«ÇÏ°Ô URI ¸¦ »ç¿ëÇÏ°íÀÚ ÇÒ °æ¿ì - ÆÄÀÏ Å©±â°¡ ºñ±³Àû ÀÛÀ» °æ¿ì¿Í ¾î¶² Á¢±Ù ±â·ÏÀ» ³²±â°íÀÚ ÇÒ °æ¿ì - ±âŸ ½ÇÁ¦ Àû¿ë ¿¹) winamp ¿ë /mp3stream/stream.php/sPy_DIR_7MDHtKu5sC5tcDM=/°ü¿ìÀÇ´«¹°.mp3 5. Èı⠽ð£ÀÌ µÈ´Ù¸é Àç¹ÌÀÖ´Â(?) ¾ÆÆÄÄ¡ Action Áö½ÃÀÚ ¼³Á¤°ú »ç¿ë¹ý¿¡ ´ëÇؼ­... EOF