如何根据.htaccess中的子域进行重定向?

时间:2009-10-06 23:53:37

标签: .htaccess mod-rewrite subdomain

htaccess的:

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{QUERY_STRING} !^id=.
RewriteRule (.*) /index.php?id=%1 [L]

预期行为:

如果有子域名(test.example.com),则转到文件夹/service/,如果没有,则转到文件夹/site/

示例行为:

test.example.com/post?id=2 -> public_html/service/post.php?id=1
test.example.com/category?id=3 -> public_html/service/category.php?id=3

example.com/register.php -> public_html/site/register.php

1 个答案:

答案 0 :(得分:0)

请尝试以下规则:

RewriteCond %{HTTP_HOST} !=www.example.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule !^service/ service/index.php?id=%1 [L]

RewriteCond %{HTTP_HOST} =example.com
RewriteRule !^site/ site%{REQUEST_URL} [L]