2011-01-04 6 views
0

htaccess를 통해 하위 도메인을 만들려고합니다. 내가 가상 만드는 방법을htaccess를 사용하여 여러 가상 하위 도메인으로 리디렉션

RewriteCond %{HTTP_HOST} ^domain.com/nl$ 
RewriteRule ^(.*) http://nl.domain.com/$1 [QSA,L,R=301] 

: 아래의 코드는 정확히 내가 확실하지 오전이이 부분입니다 그것은 http://domain.com 소요 http://www.domain.com

Options -Indexes 
DirectoryIndex index.html index.htm index.asp index.php 
ErrorDocument 401 http://www.domain.com 
ErrorDocument 403 http://www.domain.com 
ErrorDocument 404 http://www.domain.com 
ErrorDocument 500 http://www.domain.com 
ErrorDocument 507 http://www.domain.com 
RewriteEngine On 
RewriteCond %{HTTP_HOST} ^domain.com$ 
RewriteRule ^(.*) http://www.domain.com/$1 [QSA,L,R=301] 
AddType text/html .html .htm .asp 

로 리디렉션 내가

을 원하는 원하는가 누군가가 http://nl.domain.com에 가면 누군가 http://www.nl.domain.com을 입력하면 http://nl.domain.com에 머무를 것이므로 http://www.nl.domain.com을 꺼내으로 만들 수 있습니다.3210 또한 하위 도메인의 디렉토리 구조는 http://www.domain.com/nl입니다 (실제 파일이있는 곳입니다).

누군가가 http://www.domain.com/nl으로가는 경우 http://nl.domain.com으로 리디렉션해야합니다. 당신이 경우 필요한

어떤 조언에 미리 감사와 포인터 나 서버에서 위의 테스트하지 않았습니다

답변

2
RewriteEngine on 

# The ordering of the following rules is somewhat important 

# 
# External redirects with HTTP "301 - Moved Permanently" for subdomains 
# 

# Redirect www.nl.example.com to nl.example.com 
RewriteCond %{HTTP_HOST} ^www\.nl\.example\.com$ 
RewriteRule ^(.*) http://nl.example.com/$1 [QSA,L,R=301] 

# Instead I could do this to redirect any prefix before nl to nl.example.com 
# RewriteCond %{HTTP_HOST} ^.+?\.nl\.example\.com$ 
# RewriteRule ^(.*) http://nl.example.com/$1 [QSA,L,R=301] 

# Redirect www.foo.example.com to foo.example.com 
RewriteCond %{HTTP_HOST} ^www\.foo\.example\.com$ 
RewriteRule ^(.*) http://foo.example.com/$1 [QSA,L,R=301] 

# Instead I could do this to redirect any prefix before foo to foo.example.com 
# RewriteCond %{HTTP_HOST} ^.+?\.foo\.example\.com$ 
# RewriteRule ^(.*) http://foo.example.com/$1 [QSA,L,R=301] 

# Rewrite any remaining subdomains to example.com 
RewriteCond %{HTTP_HOST} !^example\.com$ 
RewriteCond %{HTTP_HOST} !^nl\.example\.com$ 
RewriteCond %{HTTP_HOST} !^foo\.example\.com$ 
RewriteRule ^(.*) http://example.com/$1 [QSA,L,R=301] 

# Assuming from this point forward we have either 
# example.com, nl.example.com, or foo.example.com as the HTTP_HOST 

# Redirect example.com/nl to nl.example.com 
# (Note that ONLY example.com/nl is caught here.) 
RewriteCond %{HTTP_HOST} ^example\.com$ 
RewriteRule ^nl(/(.*))? http://nl.example.com/$2 [QSA,L,R=301] 

# Redirect example.com/foo to foo.example.com 
# (Note that ONLY example.com/foo is caught here.) 
RewriteCond %{HTTP_HOST} ^example\.com$ 
RewriteRule ^foo(/(.*))? http://foo.example.com/$2 [QSA,L,R=301] 

# 
# Internal rewrites for directory structuring 
# 

# Internal rewrite for the nl subdomain 
# - Match the subdomain exactly 
RewriteCond %{HTTP_HOST} ^nl\.example\.com$ 
# - Check to see if the rewrite already happened (prevent 
# infinite loop of internal rewrites) 
RewriteCond %{REQUEST_URI} !^/nl(/.*|$) 
# - Rewrite the URL to the subdirectory 
RewriteRule ^(.*) /nl/$1 [L] 

# Internal rewrite for the foo subdomain 
# - Match the subdomain exactly 
RewriteCond %{HTTP_HOST} ^foo\.example\.com$ 
# - Check to see if the rewrite already happened (prevent 
# infinite loop of internal rewrites) 
RewriteCond %{REQUEST_URI} !^/foo(/.*|$) 
# - Rewrite the URL to the subdirectory 
RewriteRule ^(.*) /foo/$1 [L] 

하지만 내가 내 로컬 서버에서 테스트, 그것은에 가까워 야 너를 정확하게 이해했다.

나는 mod_rewrite docs을 본 것으로 확신합니다. 그 외에도 Rewrite GuideAdvanced Rewrite Guide에는 유용한 실습 예제가 있습니다.

+0

로버트 (Robert)는 문제 해결을위한 노력에 감사드립니다. 나는 언젠가 그것을 나중에 테스트 할 것이고 그것이 효과가 있었는지 알려줄 것이다! –

관련 문제