Apache HTTP Server Version 2.5

�� 문��� mod_rewrite
참조 문ì��ë¥� ë³´ì¶©�©ë����.
mod_rewriteë¥� �¬ì�©í���� ��ì²��� 리ë�¤ì�´ë���¸í��ê³�
�¬ë§¤������ ë°©ë��� �¤ë��©ë����. �¬ê¸°����
mod_rewrite�� �¼ë����� �¬ì�� ����ê°� ë§���
�¬í�¨ë���� ���¼ë©°, ê°�ê°��� �´ë�»ê� ��������ì§��� ���� ���¸í��
�¤ë��� ���µë����.
�´ì������ ��ë¡��� ê²��¼ë� (�´ë�)
�´ì������ ��ë¡��� ê²��¼ë� �¬ì���� (�¸ë�)
�¤ë¥¸ ��ë²�ë¡� �´ë���� ����
�������� �����¼ë�
���� ���¥ì�� ë³�ê²½ì�� ���� ���� �¸í����
��ê·� �¸ì�¤í�¸ë�
�¬ë�� ������리ì���� ���´ì� ê²���
ì§�리ì���¼ë� ë¶��°ë�� ��ë²�ë¡��� 리ë�¤ì�´ë����
��� URL
�´ë���� DocumentRoot
��� ����
쿼리 문ì���� �¬ì����ìµ�ê·¼ì�� foo.html ���´ì�ë¥�
bar.htmlë¡� �´ë��� ë³�ê²½í��ê³� �´ì�� URL��
���� �¸í���±ì�� ���� ��ê³µí��ê³��� ���¤ê� ê°����©ë����.
ê·¸ë�¬ë�� �´ì�� URL�� �¬ì�©ì��ê°� ���´ì� �´ë��� ë³�ê²½ë�� ê²���
�¸ì����ì§� 못í��ê²� ��ê³� �¶ì�µë����. ì¦�, ë¸��¼ì�°ì������
주ì��ê°� ë³�ê²½ë��ì§� ������ �©ë����.
�¤ì�� ê·�ì¹��� �µí�� �´ì�� URL�� �� URLë¡� �´ë����¼ë� �¬ì���±í�©ë����:
RewriteEngine on RewriteRule "^/foo\.html$" "/bar.html" [PT]
�¤ì�� ìµ�ê·¼ì�� foo.html ���´ì�ë¥�
bar.htmlë¡� �´ë��� ë³�ê²½í��ê³� �´ì�� URL��
���� �¸í���±ì�� ���� ��ê³µí��ê³��� ���¤ê� ê°����©ë����.
ê·¸ë�¬ë�� �´ë����� �´ì�� URL�� �¬ì�©ì����ê²� �� URL��
���¤ì£¼ê³� �¶ì�µë����. ì¦�, ë¸��¼ì�°ì���� ��ì¹� ������
ë³�ê²½ë���´ì�� �©ë����.
ë¸��¼ì�°ì���� �¬ì�©ì���� 보기ë¥� ë³�ê²½í���� �� URLë¡��� HTTP 리ë�¤ì�´ë���¸ë�� ê°����©ë����:
RewriteEngine on RewriteRule "^/foo\.html$" "bar.html" [R]
�� ���������� ���� �´ë�
������ ��ì¡°ì���¼ë� �¨ì���� Redirect ì§����´ë�� �¬ì�©í�� ��
���µë����. mod_rewrite�� �´ë�¼ì�´ì�¸í�¸ë�ë¶���
리ë�¤ì�´ë���¸ë�� �¨ê¸°ê¸� ���� �´ì�� �������� �¬ì�©ë�����µë����:
Redirect "/foo.html" "/bar.html"
������ �¤ë¥¸ ��ë²�ë¡� �´ë���� ê²½ì��, �¬ë���¤ì�� ë¶�ë§��¬ë�� ���°ì�´í�¸í���� ���� �´ì�� ��ë²����� URL�� ������ ê³��� ������기ë�� ���� �� ���µë����.
mod_rewriteë¥� �¬ì�©í���� �´ë�¬í�� URL��
�� ��ë²�ë¡� 리ë�¤ì�´ë���¸í�� �� ��ì§�ë§�, Redirect ����
RedirectMatch ì§����´ë�� �¬ì�©í���� ê²��� ê³��¤í�� ��
���µë����.
#With mod_rewrite RewriteEngine on RewriteRule "^/docs/(.+)" "http://new.example.com/docs/$1" [R,L]
#With RedirectMatch RedirectMatch "^/docs/(.*)" "http://new.example.com/docs/$1"
#With Redirect Redirect "/docs/" "http://new.example.com/docs/"
���� ���´ì� foo.html�� ���� ë³�����
foo.cgië¡� ë¸��¼ì�°ì��/�¬ì�©ì��ê°� �¸ì����ì§�
못í����ë¡� 매ë���½ê� ë³������� ë°©ë�������.
URL�� CGI �¤í�¬ë¦½�¸ë� �¬ì���±í��ê³� �¸ë�¤ë�¬ë��
cgi-scriptë¡� ê°������� CGI ��ë¡�ê·¸ë�¨ì�¼ë�
�¤í���©ë����. �´ë��ê²� ��ë©� /~quux/foo.html��
���� ��ì²��� �´ë����¼ë� /~quux/foo.cgi��
�¸ì�ë¡� �´ì�´ì�����.
RewriteEngine on RewriteBase "/~quux/" RewriteRule "^foo\.html$" "foo.cgi" [H=cgi-script]
document.YYYY����
document.XXXXë¡� ë§��´ê·¸���´ì���� ��,
��ë¥� �¤ì�� .html ���� 묶ì����
.phpë¡� ë³����� ��, URL�� ���� �¸í����ê²�
(�¬ì���� ê°����¼ë� ì¡´ì��) ë§����� ë°©ë��� 무ì������ê¹�?
�� ���¥ì��ë¥� ê°�ì§� ���� ���¼ì�� ì¡´ì�¬í��ê³� �´ì�� ���¥ì��ë¥� ê°�ì§� ���� ���¼ì�� ì¡´ì�¬í��ì§� ���� ê²½ì�°ì��ë§� �´ì�� ���¥ì������ �� ���¥ì��ë¡� URL�� �¬ì���±í�©ë����. ê·¸ë��ì§� ���¼ë©´ URL�� ë³�ê²½ë��ì§� ���µë����.
# backward compatibility ruleset for
# rewriting document.html to document.php
# when and only when document.php exists
<Directory "/var/www/htdocs">
RewriteEngine on
RewriteBase "/var/www/htdocs"
RewriteCond "$1.php" -f
RewriteCond "$1.html" !-f
RewriteRule "^(.*).html$" "$1.php"
</Directory>
�� ������ mod_rewrite�� �� �과���
기ë�¥ì�� �¬ì�©í��ë©�, ê·�ì¹� �¸í�¸ì�� �¤í�� ����ë¥� ���©í�©ë����.
�¹í�� mod_rewrite�� RewriteCond ì§����´ë��
��ê°���ê¸� ���� RewriteRule�� �¼ìª½�� 먼ì�� ��ê°��©ë����.
�°ë�¼ì�� RewriteCond ì§����´ê� ��ê°��� �� $1�� �´ë��
�����©ë����. �´ë�� �µí�� ���¼í�� 기본 ���¼ë��� �¬ì�©í����
���(document.html)� ����
(document.php) ���¼ì�� ì¡´ì�¬ë�� ���¤í�¸í�� ��
���µë����.
�� ê·�ì¹� �¸í�¸ë�� ������리ë� 컨í���¤í��(<Directory>
ë¸�ë¡� ���� .htaccess ����)���� �¬ì�©í����ë¡� �¤ê�����
-f ê²��¬ê� �¬ë�ë¥� ������ë¦� ê²½ë�ë¥� ���¸í�©ë����.
���� ì¤��� ������ë¦� 기본�� ì§�����ê¸� ���� RewriteBase ì§����´ë��
�¤ì���´ì�� �� �� ���µë����.
�´ë�� �´ê²°���� ê°��� ì¢��� ë°©ë��� mod_rewriteë¥�
���� �¬ì�©í��ì§� ��ê³�, ë¹���ê·� �¸ì�¤í�¸ë��� ���� ê°��� �¸ì�¤í�¸ì��
Redirect ì§����´ë��
배���� �������.
<VirtualHost *:80> ServerName undesired.example.com ServerAlias example.com notthis.example.com Redirect "/" "http://www.example.com/" </VirtualHost> <VirtualHost *:80> ServerName www.example.com </VirtualHost>
<If>
ì§����´ë�� �¬ì�©í���� �´ë�� ������ ���� ���µë����:
(2.4 �´ì��)
<If "%{HTTP_HOST} != 'www.example.com'">
Redirect "/" "http://www.example.com/"
</If>
���� ��ë¥� �¤ì�� �¬ì�´í�¸ì�� �¼ë�ë¥� HTTPSë¡� 리ë�¤ì�´ë���¸í���¤ë©´ �¤ì��ê³� ê°��� �� �� ���µë����:
<If "%{SERVER_PROTOCOL} != 'HTTPS'">
Redirect "/admin/" "https://www.example.com/admin/"
</If>
�´ë�� �´ì��ë¡��� �¬ì���� mod_rewriteë¥�
�¬ì�©í��ê³� �¶ë�¤ë©´ - ��ë¥� �¤ì��, �� �� RewriteRules �¸í�¸ì��
�¨ê� �¬ì�©í�´ì�� ���� ê²½ì�� - ���� ������ ì¤� ����ë¥� �¬ì�©í�� ��
���µë����.
�¬í�� 80�� ���� �¤ë¥¸ �¬í�¸ì���� �¤í������ �¬ì�´í�¸ì�� ê²½ì��:
RewriteCond "%{HTTP_HOST}" "!^www\.example\.com" [NC]
RewriteCond "%{HTTP_HOST}" "!^$"
RewriteCond "%{SERVER_PORT}" "!^80$"
RewriteRule "^/?(.*)" "http://www.example.com:%{SERVER_PORT}/$1" [L,R,NE]
그리ê³� �¬í�� 80���� �¤í������ �¬ì�´í�¸ì�� ê²½ì��:
RewriteCond "%{HTTP_HOST}" "!^www\.example\.com" [NC]
RewriteCond "%{HTTP_HOST}" "!^$"
RewriteRule "^/?(.*)" "http://www.example.com/$1" [L,R,NE]
모ë�� ��ë©��� �´ë��� ���� �´ë�� �¼ë����¼ë� ������ê³� �¶ë�¤ë©´ - ì¦�, 모ë�� ê°��¥í�� ê°��� example.com�� ���� example.com�� www.example.com�¼ë� 리ë�¤ì�´ë���¸í���¤ë©´ �¤ì�� �����¼ë�� �¬ì�©í�� �� ���µë����:
RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
RewriteCond "%{HTTP_HOST}" "!^$"
RewriteRule "^/?(.*)" "http://www.%{HTTP_HOST}/$1" [L,R,NE]
�� ê·�ì¹� �¸í�¸ë�� ì£� ��ë²� �¤ì�� ���¼ì�´ë�� ��ë²���
DocumentRoot�� ����
.htaccess ���¼ì���� 모ë�� �����©ë����.
�¹ì�� ������ �¬ë�� ��ì¹� ì¤� ������ ì¡´ì�¬í�� �� ���¼ë©°, ��ì²� �� �´ë�� ��ì¹����� ������ ì°¾ê��� �©ë����. ��ë§��� ìµ�ê·¼ì�� ������ë¦� 구조ë¥� �¬í�¸í���� ì½���ì¸�ë¥� �¬ë�� ��ì¹�ë¡� �������� ê²�������.
�¤ì�� ê·�ì¹� �¸í�¸ë�� �� ������리ì���� ������ ê²�����ê³�, �´ë�� ê³³ì������ ì°¾ì� 못í��ë©� ��ì²��� ��ì¹����� ê·¸ë��ë¡� ��ê³µí���¤ê� �����©ë����.
RewriteEngine on
# 먼� dir1/���� 찾������...
# ...찾�면 ��� �족����:
RewriteCond "%{DOCUMENT_ROOT}/dir1/%{REQUEST_URI}" -f
RewriteRule "^(.+)" "%{DOCUMENT_ROOT}/dir1/$1" [L]
# �¤ì���¼ë� dir2/���� ì°¾ì��ë´�����...
# ...찾�면 ��� �족����:
RewriteCond "%{DOCUMENT_ROOT}/dir2/%{REQUEST_URI}" -f
RewriteRule "^(.+)" "%{DOCUMENT_ROOT}/dir2/$1" [L]
# ê·¸ë��ì§� ���¼ë©´ �¤ë¥¸ Alias ���� ScriptAlias ì§����� �±ì��
# ���� ê³����©ë����.
RewriteRule "^" "-" [PT]
�¹ì�¬ì�´í�¸ì�� �¬ë�� 미ë�¬ê� ���¼ë©°, �¬ì�©ì��ê°� ��ì¹��� êµ�ê°��� ���� 미ë�¬ë� �¬ì�©ì��ë¥� 리ë�¤ì�´ë���¸í��ê³��� �©ë����.
��ì²����� �´ë�¼ì�´ì�¸í�¸ì�� �¸ì�¤í�¸ë��� ���¸í���� �´ë�� êµ�ê°����� �¤ë��ì§� ���¨í�©ë����. IP 주ì�� ì¡°í��ê°� ë¶�ê°��¥í�� ê²½ì�� 기본 ��ë²�ë¡� �´ë°±�©ë����.
RewriteMap
ì§����´ë�� �¬ì�©í���� �¬ì�©í�� ��ë²� 목ë��� ���±í�©ë����.
HostnameLookups on
RewriteEngine on
RewriteMap multiplex "txt:/path/to/map.mirrors"
RewriteCond "%{REMOTE_HOST}" "([a-z]+)$" [NC]
RewriteRule "^/(.*)$" "${multiplex:%1|http://www.example.com/}$1" [R,L]
## map.mirrors -- ë©��°í������ ë§�
de http://www.example.de/
uk http://www.example.uk/
com http://www.example.com/
##EOF##
HostNameLookupsê°�
on�¼ë� �¤ì������ ���´ì�� ��ë©�, �´ë�� ���¹í��
�±ë�� ����ë¥� ì´����� �� ���µë����.RewriteCond
ì§����´ë�� ��ì²����� �´ë�¼ì�´ì�¸í�¸ì�� �¸ì�¤í�¸ë��� ë§�ì§�ë§�
ë¶�ë¶��� êµ�ê°� ì½���ë¥� 캡ì���ê³�, �¤ì�� RewriteRule��
�´ë�� ê°��� �¬ì�©í���� ë§� ���¼ì���� ������ 미ë�� �¸ì�¤í�¸ë��
ì¡°í���©ë����.
�¼ë� �� ��ë²����� ������ ������ ���� �� �´ì���� URL�� ���µë����. ë³´í�� �¤ì��ë¡� �¬ì�©ë��ê³� ë°°í�¬ë���� ��ê·� URL�� ��ê³�, �¨ì���, �´ë��� �±ì�� URL�� ���µë����. �¬ì�©ì��ê°� ��ì²�ê³� �¨ê� �´ë�� URL�� ��ê³µí����ì§��� ê´�ê³����� ìµ�ì¢����¼ë� ë¸��¼ì�°ì�� 주ì�� ����ì¤��� ��ê·� URL�� �������´ì�� �©ë����.
모ë�� ë¹���ê·� URL�� ���� �¸ë� HTTP 리ë�¤ì�´ë���¸ë��
�������� ë¸��¼ì�°ì���� ��ì¹� 보기�� 모ë�� ���� ��ì²�����
�´ë�� �����©ë����. ���� ���� ê·�ì¹� �¸í�¸ì����
/puppies�� /canines�
��� /dogs� ��체����.
RewriteRule "^/(puppies|canines)/(.*)" "/dogs/$2" [R]
RedirectMatch "^/(puppies|canines)/(.*)" "/dogs/$2"
DocumentRoot ¶ë³´í�� �� ��ë²��� DocumentRoot��
URL "/"�� ì§��� ê´��¨ë�©ë����. ê·¸ë�¬ë�� ì¢�ì¢� �� �°ì�´í�°ë��
�¤ì��ë¡� ìµ����� �°ì�� ����ê°� ��������. ��ë¥� �¤ì��, �¬ì�´í�¸ì��
ì²��� 방문���� 방문��ê°� �¹ì�� ���� ������ë¦�
/about/�¼ë� �´ë����기ë�� ���� �� ���µë����.
�¤ì�� ê·�ì¹� �¸í�¸ë�� �¬ì�©í���� �´ë�� ������ �� ���µë����:
URL /ë¥� /about/�¼ë�
리ë�¤ì�´ë���¸í�©ë����:
RewriteEngine on RewriteRule "^/$" "/about/" [R]
�´ê��� RedirectMatch
ì§����´ë�� �¬ì�©í���¬ë�� ì²�리í�� �� ���µë����:
RedirectMatch "^/$" "http://example.com/about/"
���� �� ������ 루í�� URLë§� �¬ì���±í�©ë����. ì¦�,
http://example.com/�� ���� ��ì²�ë§� �¬ì���±í��ê³�
http://example.com/page.html�� ���� �����
�¬ì���±í��ì§� ���µë����. �¤ì��ë¡� 문ì�� 루í�¸ë�� ë³�ê²½í�� ê²½ì�� -
ì¦�, 모ë�� ì½���ì¸�ê°� �¤ì��ë¡� �´ë��
���� ������리ì�� ���� ê²½ì�� - URL�� �¬ì���±í���� ê²�ë³´ë��
DocumentRoot ì§����´ë��
ë³�ê²½í��ê±°ë�� 모ë�� ì½���ì¸�ë¥� �� ������ë¦� ��ë¡� �´ë������ ê²���
�¨ì�� ì¢��µë����.
ë²��� 2.2.16ë¶��� �´ë�� ���� FallbackResource ì§����´ë��
�¬ì�©í�´ì�� �©ë����:
<Directory "/var/www/my_blog"> FallbackResource index.php </Directory>
ê·¸ë�¬ë�� �´ì�� ë²����� Apache���� ���� ��êµ� �¬í���� �´ë³´�� �� ë³µì�¡í�� ê²½ì�� ���¼í�� ê²��� �¬ì�±í��ê¸� ���� �¤ì�� �¬ì���� �¸í�¸ì�� ë³����� �¬ì�©í�� �� ���µë����:
<Directory "/var/www/my_blog">
RewriteBase "/my_blog"
RewriteCond "/var/www/my_blog/%{REQUEST_FILENAME}" !-f
RewriteCond "/var/www/my_blog/%{REQUEST_FILENAME}" !-d
RewriteRule "^" "index.php" [PT]
</Directory>
ë°�ë©´ì��, ��ì²��� URIë¥� index.php�� 쿼리 문ì���� �¸ì��ë¡� ���¬í���¤ë©´ �´ë�� RewriteRule�� �¤ì���¼ë� ��ì²´í�� �� ���µë����:
RewriteRule "(.*)" "index.php?$1" [PT,QSA]
�� ê·�ì¹� �¸í�¸ë�� .htaccess ���¼ë�ë§� ������
<Directory> ë¸�ë¡������� �¬ì�©í�� �� ���µë����.
�� �¹ì���� ë§��� �´ê²°ì±��� �¼ì��� ê°��� %2 ��참조�� �¨ê¸°�� ���¼í�� ì¡°ê±´�� �¬ì�©í�©ë����. %1�� 쿼리 문ì���´ì�� ����(ê´��� �¤ê�ì§�)�´ê�, %3�� ��머ì�������. �� ì¡°ê±´�� ���°ì�±ì�� ���� 그리ê³� ì¹������� �´ì� '&&'�� �¼í��ê¸� ���� �¤ì�� ë³µì�¡í�©ë����.
# Remove mykey=???
RewriteCond "%{QUERY_STRING}" "(.*(?:^|&))mykey=([^&]*)&?(.*)&?$"
RewriteRule "(.*)" "$1?%1%3"
# Copy from query string to PATH_INFO
RewriteCond "%{QUERY_STRING}" "(.*(?:^|&))mykey=([^&]*)&?(.*)&?$"
RewriteRule "(.*)" "$1/products/%2/?" [PT]
# Capture the value of mykey in the query string
RewriteCond "%{QUERY_STRING}" "(.*(?:^|&))mykey=([^&]*)&?(.*)&?$"
RewriteCond "%2" !=not-so-secret-value
RewriteRule "(.*)" "-" [F]
# The desired URL might be /products/kitchen-sink, and the script expects # /path?products=kitchen-sink. RewriteRule "^/?path/([^/]+)/([^/]+)" "/path?$1=$2" [PT]