0

我有通配符子域,但是我只是不知道 mod_rewrite 到需要写这个的程度。谁能告诉我如何做到这一点,除了 www 以外的任何东西都不会进入主站点,但除此之外的任何子域都进入 /script/index.php?username=$username?

4

1 回答 1

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]
于 2008-10-23T04:20:16.983 回答