使用带有Mod_rewrite的通配符子域?

时间:2008-10-23 03:55:15

标签: mod-rewrite subdomain wildcard-subdomain

我有Wild Card子域名,但是我只是不知道编写此内容所需的mod_rewrite。任何人都可以告诉我如何使它如此除了www之外的任何东西都没有去主站点,但除了那个之外的任何子域名都转到/script/index.php?username=$username?

1 个答案:

答案 0 :(得分:1)

$ username变量应该来自哪里?

假设主站点的URL为http://www.example.com/main_site.php,并且您正在使用此外部目录上下文(即,不在.htaccess中,也不在< Directory>指令中)。如果它在.htaccess中删除了前导/(例如,将其设为main_site.php)。

我认为这不会立即起作用,因为有许多非明确的变量(用户名来自哪里?,如何处理请求的其余部分,将其作为参数传递?),这是htaccess还是主配置? ),但希望能给你一个想法:

#Turn on the rewrite engine
RewriteEngine On
#Check accessed domain, if it's either www.example.com or
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC,OR]
#example.com
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
#and the requested URL does not contain script you'll be accessing to avoid looping
RewriteCond %{REQUEST_URI} !main_site.php
#Then we tell that everything matching the above will go to main_site.php
RewriteRule ^ /main_site.php [L]
#If the request is not asking for main_site.php nor index.php
RewriteCond %{REQUEST_URI} !main_site.php
RewriteCond %{REQUEST_URI} !index.php
#We go to /script/index.php (username will be empty, becase we don't know 
#where to get it from)
RewriteRule ^ /script/index.php?username=$username [L]