작은숲:위키노트/아파치 mod rewrite 모듈

큰숲백과, 나무를 보지 말고 큰 숲을 보라.

RewriteRule에서의 QUERY_STRING과 PHP

/redirect?http://m.naver.com으로 접근하면 뒤에 붙은 URL로 리다이렉트 시키는 기능을 만들고자 한다. 하지만 쿼리 문자열로 URL을 넘겨주면 $_GET 변수에 키로 URL이 저장되고 URL에 포함된 ._으로 변경된다. 즉, $_GET['http://m_naver_com']에 빈 문자열이 들어가서 넘어온다. 이를 해결하기 위한 방법.

RewriteRule ^redirect$    ./redirect.php?target=%{QUERY_STRING} [L]

이렇게 하면 쿼리 문자열로 넘어온 값을 $_GET['target']에 값을 넣어 제대로 넘어온다.

RewriteRule의 QSA 플래그

RewriteRuleQSA 플래그를 주면 본래 넘어온 쿼리 문자열을 그대로 추가해서 넘겨주게 된다.

# Keep original query (default behavior)
RewriteRule ^page\.php$ /target.php [L]
# from http://example.com/page.php?foo=bar
# to   http://example.com/target.php?foo=bar
# Discard original query
RewriteRule ^page\.php$ /target.php? [L]
# from http://example.com/page.php?foo=bar
# to   http://example.com/target.php
# Replace original query
RewriteRule ^page\.php$ /target.php?bar=baz [L]
# from http://example.com/page.php?foo=bar
# to   http://example.com/target.php?bar=baz
# Append new query to original query
RewriteRule ^page\.php$ /target.php?bar=baz [QSA,L]
# from http://example.com/page.php?foo=bar
# to   http://example.com/target.php?foo=bar&bar=baz

참고

틀:아파치

이 작은숲 문서의 출처는 위키노트의 위키노트/아파치 mod rewrite 모듈 문서입니다.