.htaccess를 사용하여 RewriteCond로 RewriteRule 그룹화
여러 랜딩 페이지를 실행하는 데 사용하는 웹 페이지 템플릿이 있고 htaccess는 친숙한 URL을 PHP 코드 내의 페이지 ID로 연결하는 데 사용됩니다. 모두 작동하지만 현재는 모든 규칙이나 리디렉션이 충돌하기 전에 다시 쓰기 조건 줄을 추가해야합니다. 아래 의사 코드에서이를 확인할 수 있습니다.
RewriteEngine On
RewriteCond for domain1
RewriteRule for page 1 on domain 1
RewriteCond for domain1
RewriteRule for page 2 on domain 1
RewriteCond for domain2
RewriteRule for page 1 on domain 2
RewriteCond for domain2
RewriteRule for page 2 on domain 2
이것은 잘 확장되지 않으며 다른 언어를 사용하는 경우 다음과 같은 규칙을 그룹화합니다.
RewriteCond for domain1
{
RewriteRule for page 1 on domain 1
RewriteRule for page 2 on domain 1
}
RewriteCond for domain2
{
RewriteRule for page 1 on domain 2
RewriteRule for page 2 on domain 2
}
그룹화 할 수 있다면 htaccess 파일을 훨씬 더 쉽게 관리 할 수 있습니다. 규칙을 그룹화하는 방법이 있습니까? 나는 해결책을 찾으려고 노력했지만 내가 접하는 모든 예는 1 도메인에 대한 1 리디렉션에 대해 설명합니다. 내 다른 아이디어는 PHP / MySQL에서 데이터를 입력하고 버튼을 누르면 htaccess 파일을 작성하는 htaccess 스크립트를 만드는 것입니다. 읽기는 복잡하지만 괜찮습니다. 간단한 해결책이 있다고 확신합니다. 누구든지 제발 도와 줄 수 있습니까?
답변
다음은 produciton에서 작동하는 최종 솔루션입니다.
RewriteEngine On
<If "%{HTTP_HOST} == 'domain1.com'">
#main pages
RewriteRule /members/$ /index.php?pageid=2 [NC,L] RewriteRule /contact/$ /index.php?pageid=3 [NC,L]
RewriteRule /affiliates/$ /index.php?pageid=4 [NC,L] #blog articles RewriteRule /welcome-to-our-blog/$ /index.php?pageid=b0001 [NC,L]
</If>
<If "%{HTTP_HOST} == 'domain2.com'">
#main pages
RewriteRule /about/$ /index.php?pageid=2 [NC,L] RewriteRule /press-kit/$ /index.php?pageid=3 [NC,L]
RewriteRule /contact/$ /index.php?pageid=4 [NC,L] RewriteRule /affiliates/$ /index.php?pageid=5 [NC,L]
#blog articles
RewriteRule /welcome-to-our-blog/$ /index.php?pageid=b0001 [NC,L]
</If>
<If>섹션 사용에 대한 몇 가지 대안
- 조건이 일치하거나 일치하지 않는 경우 지시문 건너 뛰기
- URL 앞에 호스트 이름을 붙입니다 (일치하기 위해)
.htaccess각 호스트에 대한 별도의 구성 파일
( 제쳐두고 : 정말 대소 문자를 구분하지 NC않겠습니까--일치합니까?)
1. 조건이 일치하거나 일치하지 않을 때 지시문 건너 뛰기
S규칙 (또는 조건)이 일치하지 않는 경우 여러 지시문을 건너 뛸 수 있습니다 ( 플래그). 즉. 당신은이 경우에 따라 규칙을 건너 뛰면 HTTP_HOST되지 않습니다 domain1.com(가) 때 다음 규칙은 처리 후 HTTP_HOST입니다 domain1.com.
예를 들면 :
# Skip the following 4 rules when the host is not "domain1.com"
RewriteCond %{HTTP_HOST} !=domain1.com
RewriteRule ^ - [S=4]
# Only processed when the HTTP_HOST is equal to "domain1.com"
#main pages
RewriteRule ^members/$ /index.php?pageid=2 [NC,L] RewriteRule ^contact/$ /index.php?pageid=3 [NC,L]
RewriteRule ^affiliates/$ /index.php?pageid=4 [NC,L] #blog articles RewriteRule ^welcome-to-our-blog/$ /index.php?pageid=b0001 [NC,L]
# --------------------------------------------------------------------
# Skip the following 5 rules when the host is not "domain2.com"
RewriteCond %{HTTP_HOST} !=domain2.com
RewriteRule ^ - [S=5]
# Only processed when the HTTP_HOST is equal to "domain2.com"
#main pages
RewriteRule ^about/$ /index.php?pageid=2 [NC,L] RewriteRule ^press-kit/$ /index.php?pageid=3 [NC,L]
RewriteRule ^contact/$ /index.php?pageid=4 [NC,L] RewriteRule ^affiliates/$ /index.php?pageid=5 [NC,L]
#blog articles
RewriteRule ^welcome-to-our-blog/$ /index.php?pageid=b0001 [NC,L]
이 접근 방식의 명백한주의 사항은 S=<number>규칙을 추가하거나 제거 할 때 플래그 를 업데이트 할 때 주의해야한다는 것 입니다.
분명히 논리를 뒤집어 목차처럼 보이는 것을 맨 위에 표시 할 수 있습니다. 예를 들면 :
RewriteCond %{HTTP_HOST} =domain1.com
RewriteRule ^ - [S=2]
RewriteCond %{HTTP_HOST} =domain2.com
RewriteRule ^ - [S=5]
RewriteCond %{HTTP_HOST} =domain3.com
RewriteRule ^ - [S=9]
# --------------------------------------------------------------------
# domain1.com
#main pages
RewriteRule ^members/$ /index.php?pageid=2 [NC,L]
RewriteRule ^contact/$ /index.php?pageid=3 [NC,L] RewriteRule ^affiliates/$ /index.php?pageid=4 [NC,L]
#blog articles
RewriteRule ^welcome-to-our-blog/$ /index.php?pageid=b0001 [NC,L] # -------------------------------------------------------------------- # domain2.com #main pages RewriteRule ^about/$ /index.php?pageid=2 [NC,L]
RewriteRule ^press-kit/$ /index.php?pageid=3 [NC,L] RewriteRule ^contact/$ /index.php?pageid=4 [NC,L]
RewriteRule ^affiliates/$ /index.php?pageid=5 [NC,L] #blog articles RewriteRule ^welcome-to-our-blog/$ /index.php?pageid=b0001 [NC,L]
"건너 뛰기"규칙은 규칙이 속하는 위치에 따라 규칙을 추가하거나 제거 할 때 여러 개를 업데이트해야 할 수 있으므로 오류가 발생할 가능성이 더 높습니다.
2. URL 접두사 (일치하기 위해)를 호스트 이름과 함께 추가합니다.
또는 (일시적으로) URL 경로에 호스트 이름을 접두사로 붙이고 다음 규칙에서 이것과 일치시킬 수 있습니다. 이것은 RewriteCond모든 후속 규칙에서 추가 지시문을 피합니다 .
예를 들면 :
# Prefix URL-path with HTTP_HOST if not already prefixed / No "L" flag
RewriteCond %{HTTP_HOST}@$1 !^([a-z.-]+)@\1 RewriteCond %{REQUEST_FILENAME} !-f RewriteRule (.*) %{HTTP_HOST}/$1
# --------------------------------------------------------------------
# domain1.com
#main pages
RewriteRule ^domain1\.com/members/$ /index.php?pageid=2 [NC,L] RewriteRule ^domain1\.com/contact/$ /index.php?pageid=3 [NC,L]
RewriteRule ^domain1\.com/affiliates/$ /index.php?pageid=4 [NC,L] #blog articles RewriteRule ^domain1\.com/welcome-to-our-blog/$ /index.php?pageid=b0001 [NC,L]
# --------------------------------------------------------------------
# domain2.com
#main pages
RewriteRule ^domain2\.com/about/$ /index.php?pageid=2 [NC,L] RewriteRule ^domain2\.com/press-kit/$ /index.php?pageid=3 [NC,L]
RewriteRule ^domain2\.com/contact/$ /index.php?pageid=4 [NC,L] RewriteRule ^domain2\.com/affiliates/$ /index.php?pageid=5 [NC,L]
#blog articles
RewriteRule ^domain2\.com/welcome-to-our-blog/$ /index.php?pageid=b0001 [NC,L]
이렇게하면 추가 RewriteCond지시문을 피할 수 있지만 일치하는 URL 경로 앞에 각 경우에 도메인을 접두사로 지정해야합니다. S위의 첫 번째 예에서 와 같이 플래그를 사용하는 것보다 오류 가능성이 적습니다 .
위의 예에서 호스트 이름은 파일에 직접 매핑되지 않거나에 다시 작성되지 않은 URL에 대한 요청 끝에 URL-path 접두사로 유지됩니다 /index.php?pageid=<number>. 그러면 404가됩니다.
특별히 호스트 이름 접두사 를 제거하려는 경우 (아마도 $_SERVER['REDIRECT_URL']오류 문서에서 변수를 사용하고 $_SERVER['REQUEST_URI']있는 경우) 추가 환경 변수를 도입하고 (라고 부르겠습니다 FINISHED) 끝에서이 접두사를 제거 할 수 있습니다.
예를 들어 첫 번째 규칙을 수정하여 FINISHEDenv var (초기에는 존재하지 않음) 를 확인 하고 끝에 접두사를 제거하고 env var를 설정하는 새 규칙을 도입합니다. FINISHEDenv var가 없으면 "알 수없는 URL"에 대한 재 작성 루프가 발생합니다. URL 경로에서 호스트 이름의 접두사를 반복적으로 제거하고 제거하기 때문입니다.
# Prefix URL-path with HTTP_HOST if not already prefixed and not FINISHED / No "L" flag
RewriteCond %{ENV:FINISHED} ^$
RewriteCond %{HTTP_HOST}@$1 !^([a-z.-]+)@\1 RewriteCond %{REQUEST_FILENAME} !-f RewriteRule (.*) %{HTTP_HOST}/$1
# :
# : Directives for each domain go here (as above)
# :
# Tidy up - remove the HTTP_HOST prefix and set FINISHED env var
RewriteCond %{HTTP_HOST}@$1 ^([a-z.-]+)@\1/(.*)
RewriteRule (.*) %2 [E=FINISHED:1,L]
3. .htaccess각 호스트에 대한 별도의 구성 파일
별도의 도메인과 본질적으로 별도의 웹 사이트 (아마도 "공통" 엔진 임에도 불구하고)의 이러한 특정 상황에 더 적합한 또 다른 대안은 .htaccess각 사이트에 대해 추가 (별도의) 파일을 갖는 것입니다.
이를 구성하는 방법은 정확히 파일 구조를 구성한 방법에 따라 달라질 수 있지만,이를 통해 사이트 간 분리를 늘릴 수 있으며 원하는 경우 두 개 이상의 사이트간에 구성을 공유 할 수도 있습니다. 액세스중인 사이트와 관련된 리소스가있을 수 있지만 이미 이와 같은 것이있을 수 있습니다.
예를 들면 ...
.htaccess문서 루트 에는 여전히 "마스터" 파일이 있습니다 (모든 도메인에 대해 공통 루트 디렉토리를 공유한다고 가정).- 각 호스트에 대한 하위 디렉토리를 만듭니다. 예.
/domain1.com각 하위 디렉토리 안에는.htaccess해당 도메인에 대한 파일이 있습니다.
파일 구조 :
/
domain1.com/
.htaccess
domain2.com/
.htaccess
.htaccess
index.php
/.htaccess
.htaccess그런 다음 루트 파일은 요청 된 호스트의 적절한 하위 디렉터리로 요청을 라우팅합니다.
RewriteEngine On
# If the request is already for index.php in the document root then DONE
RewriteRule ^index\.php$ - [L]
# Rewrite all requests that don't map to files to the appropriate domain subdirectory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) %{HTTP_HOST}/$1 [L]
/domain1.com/.htaccess
그런 다음 각 도메인의 .htaccess파일은 해당 도메인에 대한 지시문 만 포함하고 요청 /index.php을 렌더링하기 위해 문서 루트 의 "공통" 파일에 요청을 다시 작성합니다 .
재 작성 지시문은 위에서 사용 된 것과 동일한 "일반"형식을 사용합니다.
# domain1.com
RewriteEngine On
# Optional - If this subdirectory is accessed directly then redirect back to the root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) /$1 [R=301,L] #main pages RewriteRule ^members/$ /index.php?pageid=2 [NC,L]
RewriteRule ^contact/$ /index.php?pageid=3 [NC,L] RewriteRule ^affiliates/$ /index.php?pageid=4 [NC,L]
#blog articles
RewriteRule ^welcome-to-our-blog/$ /index.php?pageid=b0001 [NC,L]
어쨌든, 생각할 음식. 이 중 일부는 엄격하게 "RewriteCond에 의한 RewriteRule 그룹화"가 아니라 응용 프로그램을 구조화하는 방법에 달려 있습니다. "비트"에 대한 추가 설명이 필요하면 말하십시오.