작은숲:위키노트/아파치 mod rewrite 모듈: 두 판 사이의 차이
잔글 (문자열 찾아 바꾸기 - "\[\[분류:공유(\|.*?)?\]\]" 문자열을 "분류:위키노트/공유$1" 문자열로) |
잔글 (→RewriteRule에서의 QUERY_STRING과 [[작은숲:위키노트/PHP|PHP]]: move articles, replaced: PHP → PHP (2)) |
||
| 1번째 줄: | 1번째 줄: | ||
{{DISPLAYTITLE:아파치 mod_rewrite 모듈}} | {{DISPLAYTITLE:아파치 mod_rewrite 모듈}} | ||
== RewriteRule에서의 QUERY_STRING과 [[위키노트 | == RewriteRule에서의 QUERY_STRING과 [[작은숲:위키노트/PHP|PHP]] == | ||
<code><nowiki>/redirect?http://m.naver.com</nowiki></code>으로 접근하면 뒤에 붙은 [[위키노트 | <code><nowiki>/redirect?http://m.naver.com</nowiki></code>으로 접근하면 뒤에 붙은 [[작은숲:위키노트/URL|URL]]로 리다이렉트 시키는 기능을 만들고자 한다. 하지만 쿼리 문자열로 URL을 넘겨주면 <code>$_GET</code> 변수에 키로 URL이 저장되고 URL에 포함된 <code>.</code>은 <code>_</code>으로 변경된다. 즉, <code><nowiki>$_GET['http://m_naver_com']</nowiki></code>에 빈 문자열이 들어가서 넘어온다. 이를 해결하기 위한 방법. | ||
<syntaxhighlight lang="apache"> | <syntaxhighlight lang="apache"> | ||
RewriteRule ^redirect$ ./redirect.php?target=%{QUERY_STRING} [L] | RewriteRule ^redirect$ ./redirect.php?target=%{QUERY_STRING} [L] | ||
2022년 5월 7일 (토) 13:53 판
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 플래그
RewriteRule에 QSA 플래그를 주면 본래 넘어온 쿼리 문자열을 그대로 추가해서 넘겨주게 된다.
# 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
참고
- Manipulating the Query String
- Moving path information to a query string - simple
- Apache RewriteRule and query string