[킴스큐 Rb]킴스큐 Rb .htaccess 파일의 디폴트 룰
Rewrite 룰에 대한 기본 개념을 알아봤으니 킴스큐 Rb .htaccess 룰을 하나하나 짚어봐야겠다.
아래는 킴스큐 Rb .htaccess의 디폴트 룰임 (킴스큐 Rb 버전 1.2.0 기준)
---------------------------------------------------------------------------------
RewriteEngine On
#기본사이트
RewriteRule ^(admin)/?$ ./index.php?m=$1&module=$1 [L]
RewriteRule ^(p)/([a-zA-Z0-9_\-]+)/?$ ./index.php?mod=$2 [L]
RewriteRule ^(c)/([a-zA-Z0-9_\-\/]+)/?$ ./index.php?c=$2 [L]
RewriteRule ^([0-9]+)/?$ ./index.php?m=bbs&uid=$1 [L]
RewriteRule ^(b)/?$ ./index.php?m=bbs [L]
RewriteRule ^(b)/([a-zA-Z0-9_\-]+)/?$ ./index.php?m=bbs&bid=$2 [L]
RewriteRule ^(b)/([a-zA-Z0-9_\-]+)/([0-9]+)/?$ ./index.php?m=bbs&bid=$2&uid=$3 [L]
RewriteRule ^(b)/([a-zA-Z0-9_\-]+)/write/?$ ./index.php?m=bbs&bid=$2&mod=write [L]
RewriteRule ^(b)/([a-zA-Z0-9_\-]+)/([0-9]+)/s([0-9]+)/?$ ./index.php?m=bbs&bid=$2&uid=$3&s=$4 [L]
RewriteRule ^(b)/([a-zA-Z0-9_\-]+)/([0-9]+)/([0-9]+)/?$ ./index.php?m=bbs&bid=$2&uid=$3&CMT=$4#CMT [L]
RewriteRule ^(b)/([a-zA-Z0-9_\-]+)/([0-9]+)/([0-9]+)/s([0-9]+)/?$ ./index.php?m=bbs&bid=$2&uid=$3&CMT=$4&s=$5#CMT [L]
#사이트코드 확장
RewriteRule ^([a-zA-Z0-9_\-]+)/?$ ./index.php?r=$1 [L]
RewriteRule ^([a-zA-Z0-9_\-]+)/(admin)/?$ ./index.php?r=$1&m=$2&mdule=$2 [L]
RewriteRule ^([a-zA-Z0-9_\-]+)/(p)/([a-zA-Z0-9_\-]+)/?$ ./index.php?r=$1&mod=$3 [L]
RewriteRule ^([a-zA-Z0-9_\-]+)/(c)/([a-zA-Z0-9_\-\/]+)/?$ ./index.php?r=$1&c=$3 [L]
RewriteRule ^([a-zA-Z0-9_\-]+)/([0-9]+)/?$ ./index.php?r=$1&m=bbs&uid=$2 [L]
RewriteRule ^([a-zA-Z0-9_\-]+)/(b)/?$ ./index.php?r=$1&m=bbs [L]
RewriteRule ^([a-zA-Z0-9_\-]+)/(b)/([a-zA-Z0-9_\-]+)/?$ ./index.php?r=$1&m=bbs&bid=$3 [L]
RewriteRule ^([a-zA-Z0-9_\-]+)/(b)/([a-zA-Z0-9_\-]+)/([0-9]+)/?$ ./index.php?r=$1&m=bbs&bid=$3&uid=$4 [L]
RewriteRule ^([a-zA-Z0-9_\-]+)/(b)/([a-zA-Z0-9_\-]+)/write/?$ ./index.php?r=$1&m=bbs&bid=$3&mod=write [L]
RewriteRule ^([a-zA-Z0-9_\-]+)/(b)/([a-zA-Z0-9_\-]+)/([0-9]+)/s([0-9]+)/?$ ./index.php?r=$1&m=bbs&bid=$3&uid=$4&s=$5 [L]
RewriteRule ^([a-zA-Z0-9_\-]+)/(b)/([a-zA-Z0-9_\-]+)/([0-9]+)/([0-9]+)/?$ ./index.php?r=$1&m=bbs&bid=$3&uid=$4&CMT=$5#CMT [L]
RewriteRule ^([a-zA-Z0-9_\-]+)/(b)/([a-zA-Z0-9_\-]+)/([0-9]+)/([0-9]+)/s([0-9]+)/?$ ./index.php?r=$1&m=bbs&bid=$3&uid=$4&CMT=$5&s=$6#CMT [L]
Regex vocabulary (정규표현식 어휘)
The following are the minimal building blocks you will need, in order to write regular expressions and RewriteRule
s. They certainly do not represent a complete regular expression vocabulary, but they are a good place to start, and should help you read basic regular expressions, as well as write your own.
아래는 당신이 정규 표현식과 RewriteRule을 사용할 수 있는데에 필요한 최소한의 구성 요소이다. 물론 아래의 구성 요소는 정규 표현식의 모든 어휘를 포함하는 것은 절대 아니지만 최소한 당신이 기초적인 정규 표현식을 읽을 수 있고 또한 직접 작성할 수 있게 도와줄 첫 걸음이 될 것이다.
Character (문자) | Meaning (의미) | Example (예제) |
---|---|---|
. | Matches any single character | c.t will match cat , cot , cut , etc.(c.t는 cat, cot, cut 등에 상응한다.) |
+ | Repeats the previous match one or more times (문자가 한 번 또는 그 이상 상응함을 나타낸다) | a+ matches a , aa , aaa , etc(a+는 a, aa, aaa등에 상응한다) |
* | Repeats the previous match zero or more times. (문자가 0번 혹은 그 이상 상응함을 나타낸다) | a* matches all the same things a+ matches, but will also match an empty string.(a*는 a+에 상응하는 모든 문자에 상응하고 또한 빈 문자에도 상응한다) |
? | Makes the match optional. (선택적 상응을 나타낸다. 즉, 있어도 되고 없어도 됨) | colou?r will match color and colour .(colou?r는 color와 colour에 상응한다) |
^ | Called an anchor, matches the beginning of the string (앵커라고 불리우며 스트링의 시작을 나타낸다) | ^a matches a string that begins with a |
$ | The other anchor, this matches the end of the string. (스트링의 끝을 나타내는 또 다른 앵커) | a$ matches a string that ends with a .(a$는 a로 끝나는 스트링에 상응한다) |
( ) | Groups several characters into a single unit, and captures a match for use in a backreference. | (ab)+ matches ababab - that is, the + applies to the group. For more on backreferences see below.( (ab)+는 ababab에 상응한다. 이 말은 +가 묶음에 적용된다는 뜻이다. backreference에 대한 설명은 아래 참고) |
[ ] | A character class - matches one of the characters | c[uoa]t matches cut , cot or cat .( c[uoa]t 는 cut, cot, cat에 상응한다) |
[^ ] | Negative character class - matches any character not specified | c[^/]t matches cat or c=t but not c/t |
In mod_rewrite
the !
character can be used before a regular expression to negate it. This is, a string will be considered to have matched only if it does not match the rest of the expression.
mod_rewrite에서 ! 문자는 정규식 앞에 사용되면 부인한다는(반대된다는) 의미이다. 즉, 스트링이 정규식에 상응하지 않을 때 정규식 앞에 !을 붙임으로써 상응하게 되게 것이다.
RewriteEngine On
-> Rewrite 모듈 호출
#기본사이트
RewriteRule ^(admin)/?$ ./index.php?m=$1&module=$1 [L]
The [L] flag causes mod_rewrite
to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed. This corresponds to the last
command in Perl, or the break
command in C. Use this flag to indicate that the current rule should be applied immediately without considering further rules.
[L] 플래그는 mod_rewrite
에게 룰 세트에 대한 처리를 중단시킨다. 대부분의 컨텍스트에서 이것은 만약 매칭되는 룰이 있으면 이후에 오는 룰에 대한 처리는 중단된다는 뜻이다. 이것은 Perl에서의 last
명령어, C에서의 명령어와 break
명령어와 일치한다. 이후에 오는 룰을 고려하지 않고 현재 룰을 바로 적용시키기 위해 이 플래그를 사용하라.
If you are using RewriteRule
in either .htaccess
files or in <Directory>
sections, it is important to have some understanding of how the rules are processed. The simplified form of this is that once the rules have been processed, the rewritten request is handed back to the URL parsing engine to do what it may with it. It is possible that as the rewritten request is handled, the .htaccess
file or <Directory>
section may be encountered again, and thus the ruleset may be run again from the start. Most commonly this will happen if one of the rules causes a redirect - either internal or external - causing the request process to start over.
만약 RewriteRule
을 .htaccess
파일이나 <Directory>
섹션에서 사용한다면, 이 룰들이 어떻게 처리가 되는지 이해하는 것이 중요하다. 이것의 간단한 형태는 - 일단 룰이 처리가 되면, 재작성된 리퀘스트는 URL 파싱 엔진에게 되돌려 나머지 작업을 시킨다. 이때 재작성된 리퀘스트 작업이 이루어지면서 .htaccess
파일이나 <Directory>
섹션이 다시 불러질 수 있고 이때 룰 세트가 처음부터 다시 시작될 수 있다. (무한루프 발생 가능성!!!) 대부분의 경우에 이것은 룰 중 하나가 리다이렉팅(내부적이든 외부적이든)을 실행하여 리퀘스트 프로세스를 처음부터 다시 시작하게 할 때 발생한다.
It is therefore important, if you are using RewriteRule
directives in one of these contexts, that you take explicit steps to avoid rules looping, and not count solely on the [L] flag to terminate execution of a series of rules, as shown below.
그러므로 RewriteRule
명령을 이러한 컨텍스트들 중 하나에서 사용한다면, 룰의 무한루핑을 방지하기 위해 단순히 [L] 플래그를 사용하여 룰의 실행을 종료하는 것에만 의존하지 말고 아래와 같이 확실한 절차를 밟아줘야 한다. ([L]에서 L은 Last의 약자인듯)
An alternative flag, [END], can be used to terminate not only the current round of rewrite processing but prevent any subsequent rewrite processing from occuring in per-directory (htaccess) context. This does not apply to new requests resulting from external redirects.
대체 플래그인 [END]는 rewrite 프로세스의 현재 룰뿐 아니라 이후의 디렉토리별 (htaccess) 컨텍스트에서의 rewrite 프로세싱까지 종료할 때 사용될 수 있다. 이것은 외부 리다이렉트에서 발생되는 새로운 리퀘스트에 적용되지 않는다.
The example given here will rewrite any request to index.php
, giving the original request as a query string argument to index.php
, however, the RewriteCond
ensures that if the request is already for index.php
, the RewriteRule
will be skipped.
아래 예제는 오리지널 리퀘스트를 index.php에 대한 쿼리 스트링 인자로 하여 index.php으로의 어떠한 리퀘스트라도 리라이팅한다. 하지만, 만약 리퀘스트가 이미 index.php에 대한 것이라면 RewriteCond은 스킵된다. (예제에서 [PT]는 Pass Through의 뜻 - 간략 설명: The target (or substitution string) in a RewriteRule is assumed to be a file path, by default. The use of the [PT] flag causes it to be treated as a URI instead. - 자세한 설명은: http://httpd.apache.org/docs/2.4/en/rewrite/flags.html#flag_pt 참고)
RewriteBase / RewriteCond %{REQUEST_URI} !=/index.php RewriteRule ^(.*) /index.php?req=$1 [L,PT]
RewriteRule ^(p)/([a-zA-Z0-9_\-]+)/?$ ./index.php?mod=$2 [L]