mod_rewriteã®èãæ¹ã
ãè¦ãªããã
- URLãæ£è¦è¡¨ç¾(A)ã«ããããããã¤
- æåå(B)ãæ¡ä»¶(C)ãæºããå ´åã«ã
- URLã(D)ã«æ¸ãæããã
ã¨ããã®ãåºæ¬ã
RewriteRule
URLã(A)ã®æ£è¦è¡¨ç¾ã«ãããããã(D)ã§æ¸ãæããã
æ£è¦è¡¨ç¾(A)ã¯ããªã©ã¤ããå®è¡ãããã©ããã®æ¡ä»¶(çå½å¤)ã§ãã£ã¦ãç½®æ url =~ s/(A)/(D)/ ã¨ãããã¨ã§ã¯ãªãããã¨ãã°ã以ä¸ã®ãããªæ£è¦è¡¨ç¾ã§ãªã©ã¤ããããã
# 1æåãããããããªã©ã¤ãå®è¡ã空æååã§ãªããã°å®è¡ããã RewriteRule . index.php [L]
# å é ã«ãããããããªã©ã¤ãå®è¡ã常ã«å®è¡ããã RewriteRule ^ - [L]
ããè¦ãããã®ã¯ .* ã ãã$0ã§å¾æ¹åç §ããã®ã§ãªããã°å ¨ä½ã«ãããããå¿ è¦ã¯ãªãã¨æããå é ã«ããã^ã§ãååãªã¯ããçç±ããããªãç¥ãããã
# dispatch.phpã¸ã®ã¢ã¯ã»ã¹ã§ãªããã°dispatch.phpã«ã¢ã¯ã»ã¹ãããQSAã§ã¯ã¨ãªã¼æååã足ãã.*ã§å ¨ä½ãããããããå¿ è¦ããï¼ RewriteCond %{REQUEST_URI} !dispatch\.php$ RewriteRule .* dispatch.php [L,QSA]
# èªè¨¼ã¦ã¼ã¶ãç°å¢å¤æ°ã«è¨å®ã.*ã§å ¨ä½ãããããããå¿ è¦ããï¼ RewriteEngine on RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization},L]
^(.*)$ã大éã«è¦ã¤ããããã«ãã³ã®æå³ã¯ãããã ãããã
# .*ã()ãç¡é§ã§ã¯ï¼ RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
# .*ã()ãç¡é§ã§ã¯ï¼ RewriteRule ^(.*)$ ./inc/bx/php/UserFile.php?file=$1
$1ã®ä»£ããã«$0ã使ãããã%{REQUEST_URI}ã使ããã
RewriteRule .* index.php/$0 [PT,L]
# mod_balancerã¨ã®é£æºããã¡ã¤ã«ãç¡ãã£ããmongrel_clusterã«éãã # .*ã()ãç¡é§ã§ã¯ï¼ RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
ã¡ãªã¿ã«ãabã§ãã³ããã¼ã¯ãåã£ã¦ã¿ããã^ã§ã^(.*)ã§ãé度ã¯ã¾ã£ããå¤ãããªãã£ããç¡æå³ãª () ã¯ã©ããã¨æãã .* ã¯èªã¿ãããã¦ããã®ããã
(D)ã«æå®ã§ããã®ã¯
- ãã¡ã¤ã«ã®ãã¹ /www/index.html
- URLã®ãã¹ /www/index.html ãã¡ã¤ã«ã®ãã¹ã¨åºå¥ãã¤ããªããâ/wwwããã¡ã¤ã«ã·ã¹ãã ã«åå¨ãããã©ããã§å¤æã
- 絶対URL http://www.example.com/example.html â ãã¹ãåãä¸è´ãããåãé¤ãã¦URLãã¹ã«ãã§ãªããã°ãªãã¤ã¬ã¯ãããããã·ã
- ãã¤ãã³(ç½®æããªã) âãã©ã°ã®æä½ã ããå®è¡ãããå ´åã«ä½¿ãã
ãã©ã°[F1]ã§URLã®ç½®æãè¶ ããæä½ãæå®ã§ããã
- L æã¡æ¢ãããã®URLã«ã¯ãã以ä¸RewriteRuleãé©ç¨ããªãã
- R ãªãã¤ã¬ã¯ã
- P ãããã·
ã¨ãã
RewriteCond
æ£è¦è¡¨ç¾(A)ã«è¿½å ããæ¡ä»¶ãè¤æ°åæå®ã§ããã
- URLã(A)ã«ããããããã¤ãRewriteCondãå ¨ã¦æºãããã(D)ã«æ¸ãæããã
- (B)ã¯HTTPããããç°å¢å¤æ°ãæ£è¦è¡¨ç¾(A)(C)ã®å¾æ¹åç § $1/%1 ãªã©ã
- (C)ã¯æ£è¦è¡¨ç¾ãæååã®æ¯è¼<=>ããã¡ã¤ã«ãã¹ã(-f)ãªã©ã
ãã©ã°[F2]ã§æ¡ä»¶ã®åä½ãªãã·ã§ã³ãæå®ã§ããã
- OR é常ANDã§æ¥ç¶ããã¨ãããORã§ã¤ãªãã
- NC 大æåå°æåãåºå¥ããªãã
ãã¡ã¤ã³åã®æ£è¦å
www.example.jp 㨠example.jp 㨠www.example.com ã example.com ã«ãªãã¤ã¬ã¯ããããå ´åãååãã¼ã¹ã®ãã¼ãã£ã«ãã¹ããªãã
<VirtualHost *:80> ServerName example.com ServerAlias example.jp ServerAlias www.example.com ServerAlias www.example.jp RewriteEngine On RewriteCond %{HTTP_HOST} !^example\.com$ [NC] RewriteRule ^/(.*) http://example.com/$1 [L,R=301,NE] </VirtualHost>
ã®ããã«è¨å®ããã
以ä¸ã«Apacheãªã¹ã¹ã¡ã®è¨å®(以ä¸ã¬ã¤ãçã¨å¼ã¶)ããããä¸ã¯ãã®ã¬ã¤ãçãä¿®æ£ãããã®ã
RewriteCond %{HTTP_HOST} !^fully\.qualified\.domain\.name [NC] RewriteCond %{HTTP_HOST} !^$ RewriteRule ^/(.*) http://fully.qualified.domain.name/$1 [L,R,NE]
åãã©ã°ã®æå³ã¯ã
- NC 大æåå°æåãç¡è¦
- L å¤ææã¡åã(確å®)
- R ãªãã¤ã¬ã¯ã
- NE ã¨ã¹ã±ã¼ãããªã
1è¡ç®ãå¦å®ã® ! ããã¤ã³ããæ£è¦ãã¡ã¤ã³å以å¤ã«ãããããå ´åã«æ¸ãæãããå¦å®ãã¿ã¼ã³ãæ¸ãæ¹ãã·ã³ãã«ã§æ´æ°ã楽ãéæ£è¦ãã¡ã¤ã³åãServerAliasã§å¥½ããªã ã足ããã
2è¡ç®ã¯ã²ã¨ã¾ãç½®ãã¦ããã¦ã3è¡ç®ãNEããã¤ã³ããNEãä»ããªãã¨ã
ã«ãªãã¤ã¬ã¯ãããã¦ãã¾ãã%ã%25ã«ã¨ã¹ã±ã¼ããããã®ãæ¢ããã«ã¯ãNEãå¿ è¦ã
ä¾ãã°ãä»æ¥(2009-08-31)ç¾å¨ã
ã«ãªãã¤ã¬ã¯ãããããNEã®ä»ãå¿ãã§ã¯ã
ä¿®æ£ç®æ
ã¬ã¤ãçããä¿®æ£ããã®ã¯ã
- 1è¡ç®æ£è¦è¡¨ç¾ã®æ«å°¾ã« $ ã足ããã
- 2è¡ç®ãæ¶ããã
- 3è¡ç®ãRã®ã¹ãã¼ã¿ã¹ã³ã¼ãã301(Moved Permanently)ã«è¨å®ããã
ã®3ã«æã
2è¡ç®ã®è¬
ã¬ã¤ãçã®2è¡ç®ãããããããªãã£ãã
RewriteCond %{HTTP_HOST} !^$
空ã®HTTP_HOSTã¯ç½®æ対象ããå¤ãã¦ããã
HTTP_HOSTã¯ã©ããªå ´åã«ç©ºã«ãªãã®ããã½ã¼ã¹ãè¦ãã¨ã
HTTP_HOSTã«ã¯ãHostãããã®å¤ãè¨å®ããã¦ããããã ã
èæ ®ãã¹ããã¨ã
- HTTP/1.0ã§ã¯Hostãããã¯éãããªããããããªãã
- IE2ã¯éããªãã£ã(ããã)
- HTTP/1.1ã§ã¯Hostãããã¯å¿
é ã
- ãã ããããããªã¯ã¨ã¹ãããã URI ããªã¯ã¨ã¹ãããã¦ãããµã¼ãã¹ã®ã¤ã³ã¿ã¼ããããã¹ãåãå«ãã§ããªããã°ãHost ããããã£ã¼ã«ãã®å¤ã¯ç©ºã«ããªããã°ãªããªãããããã¯ã©ãããå ´åï¼
- ハイパーテキスト転送プロトコル -- HTTP/1.1 14.23 Host
- åæ: If the requested URI does not include an Internet host name for the service being requested, then the Host header field MUST be given with an empty value.
- HTTP/1.1: Header Field Definitions 14.23 Host
- ãã ããããããªã¯ã¨ã¹ãããã URI ããªã¯ã¨ã¹ãããã¦ãããµã¼ãã¹ã®ã¤ã³ã¿ã¼ããããã¹ãåãå«ãã§ããªããã°ãHost ããããã£ã¼ã«ãã®å¤ã¯ç©ºã«ããªããã°ãªããªãããããã¯ã©ãããå ´åï¼
- Request-URIãAbsoluteURIã®å ´åãHTTP/1.1ã®ä»æ§ã§hostãããã¯ç¡è¦ãããããºã
- ハイパーテキスト転送プロトコル -- HTTP/1.1 5.1.2, 5.2
- Hostããããéããããªãã¤ã¬ã¯ããæ£å¸¸ã«å¦çããã¯ã©ã¤ã¢ã³ãããã£ãå ´åãHTTP_HOSTã空ã®å ´åãé¤å¤ããªãã¨ãã«ã¼ããããããããªãã
ç¹ã«ãã®ãã¡ã¤ã³ã§ã¢ããªã±ã¼ã·ã§ã³ãåããã¦ããå ´åãHostãããã®æç¡ãèæ ®ãããã¹ããªããã§ããã¯ãããªããHostãããããªããªã¯ã¨ã¹ãã«ã¯å¥ã®ã³ã³ãã³ããè¿ãã¦ããã®ãããã
ã¨ããçç±ã§ãApacheè¨å®ã®2è¡ç®ã¯å¤ããã
RewriteRuleãé£éããã¨ãå ¥åã空æååã«ãªããã¨ãããã
RewriteRuleã§URLã空æååã«æ¸ãæããããã
RewriteRule . "" # URLã空æååã«æ¸ãæãã RewriteRule . /abc.html [L] # ä»»æã®1æåããã®ã«ã¼ã«ã«ã¯ãããããªãã RewriteRule .* /xyz.html [L] # 0æå以ä¸ãããã«ãããããã[L]ã§çµäºã
ä¸çªä¸ã®RewriteRuleã§ç©ºæååã«ãªãã2è¡ç®ã«ã¯ãããããªãã3è¡ç®ã®/xyz.htmlã«ãªã¯ã¨ã¹ããéãããã
RewriteRule ^ ""
ã ãã ã¨400 Bad Requestã«ãªã£ã¦ãããRewriteLogã§ç©ºæååã«æ¸ãæãã£ã¦ããã®ã確èªã§ããã
127.0.0.1 - - [14/Jun/2009:05:44:38 +0900] [default.local/sid#1008de948][rid#10096fe98/initial] (3) applying pattern '^' to uri '/index.html' 127.0.0.1 - - [14/Jun/2009:05:44:38 +0900] [default.local/sid#1008de948][rid#10096fe98/initial] (2) rewrite '/index.html' -> '' 127.0.0.1 - - [14/Jun/2009:05:44:38 +0900] [default.local/sid#1008de948][rid#10096fe98/initial] (2) local path result:
REQUEST_FILENAMEã®ãã¾
ããã¥ã¡ã³ãã«%{REQUEST_FILENAME}ã¯ãã¡ã¤ã«ã·ã¹ãã ã®ãã«ãã¹ã ã¨ãããããªãã¨ãæ¸ãã¦ãããæ£ãããªããã³ã³ããã¹ãã«ãã£ã¦REQUEST_FILENAMEã®å 容ãå¤ããããããã
ãã°ãã©ãã¯ã§ã¯ä»¥åããè¨ããã¦ããã
- Bug 46376 – %{REQUEST_FILENAME} does not hold full local filesystem path to the file or script matching the request 2008/12
- Bug 45280 – REQUEST_FILENAME is not full local filesystem path 2008/06
- Bug 16402 – %{REQUEST_FILENAME} needs lookahead in order to contain local path instead of URI 2003/01
ãã®è¨äºã¯æ¸ãããã§ãã