使用htaccess重定向到多个虚拟子域

时间:2011-01-04 13:14:44

标签: .htaccess

我正在尝试通过htaccess创建子域名。下面的代码确实想要我想要

需要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

这是我不确定的部分:

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

如何创建虚拟子域名,以便如果有人前往http://nl.domain.com,如果有人输入http://nl.domain.com,它会留在http://www.nl.domain.com,它会取出http://www.nl.domain.com并进行制作http://nl.domain.com子域的目录结构也是http://www.domain.com/nl(这是实际文件所在的位置)。

因此,如果有人转到http://www.domain.com/nl,则还应该重定向到http://nl.domain.com

提前感谢任何建议和指示

1 个答案:

答案 0 :(得分: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也提供了有用的实际示例。