目的:动态解析home.sst.cn的下级域名,username.home.sst.cn,实现基于虚拟域名(动态域名)的个人空间访问 。
b) 如果是Apache,则修改httpd.conf文件加入虚拟主机(如果需要同时供应多个web服务)
ServerAlias *.test.com
分析访问过程:
用户输入[username].test.com
|
|
|
`-->apache分析主机头的值,不匹配任何一
个虚拟主机名,则交送中心主机的目录
|
|
|
接下来应该有两种方法处理:<--'
|
|
^
' `
/ \
① 在home.test.com的根目录 ② 用mod_rewrite重写
设跳转文件,ASP代码举例: URL指向[username]目录。
| |
| |
| |
方法①纯代码方式,无用户目录。
方法②用mod_rewrite重写URL,指向用户目录。
在blog的主目录下建立.htaccess文件,内容为:
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteRule ^(.*)/home/(.*)$ $1.php?$2
</IfModule>
实现步骤:
1.DNS的泛域名解析
nbsp; IN SOA ns.sst.cn. zhao.sst.cn. (
20050817 ; Serial
3600 ; Refresh
900 ; Retry
3600000 ; Expire
3600 ) ; Minimum
IN NS ns.sst.cn.
................................................
home IN A 123.123.123.123
*.home IN A 123.123.123.123
20050817 ; Serial
3600 ; Refresh
900 ; Retry
3600000 ; Expire
3600 ) ; Minimum
IN NS ns.sst.cn.
................................................
home IN A 123.123.123.123
*.home IN A 123.123.123.123
以上是在unix bind9下的设置,在windows server中的DNS设置与之同理,略。
2. web服务的设置
a) 如果是IIS,只需要在IIS中设置一个空的主机头,然后置一跳转文件,例:
[asp代码片断]
<% dim temp1, temp2, temp3
temp1 = Request.ServerVariables("HTTP_HOST")
temp2 = Right(Request.ServerVariables("HTTP_HOST"),12) ' 其中 12 表示你的域名去掉前辍后剩下的字符数 .
temp3 = Replace(temp1,temp2,"")
%>
<%if temp3 <> "www" then
Response.Redirect "http://home.sst.cn/index.asp?name=" & temp3 %>
<%end if%>
temp1 = Request.ServerVariables("HTTP_HOST")
temp2 = Right(Request.ServerVariables("HTTP_HOST"),12) ' 其中 12 表示你的域名去掉前辍后剩下的字符数 .
temp3 = Replace(temp1,temp2,"")
%>
<%if temp3 <> "www" then
Response.Redirect "http://home.sst.cn/index.asp?name=" & temp3 %>
<%end if%>
b) 如果是Apache,则修改httpd.conf文件加入虚拟主机(如果需要同时供应多个web服务)
<VirtualHost *:80>
ServerAdmin zhao@test.com
ServerAdmin zhao@test.com
DocumentRoot /
test.com
ServerName
test.com
ServerAlias *.test.com
ErrorLog logs/ test.com-error_log
CustomLog logs/ test.com-access_log common
</VirtualHost>
<VirtualHost *:80>
ServerName www. test.com
DocumentRoot / test.com/www
</VirtualHost>
为改变虚拟主机顺序,使提供泛域名的web服务为非中心主机(main host),加入此句:ServerAlias *.test.com
ErrorLog logs/ test.com-error_log
CustomLog logs/ test.com-access_log common
</VirtualHost>
<VirtualHost *:80>
ServerName www. test.com
DocumentRoot / test.com/www
</VirtualHost>
ServerAlias *.test.com
分析访问过程:
用户输入[username].test.com
|
|
|
`-->apache分析主机头的值,不匹配任何一
个虚拟主机名,则交送中心主机的目录
|
|
|
接下来应该有两种方法处理:<--'
|
|
^
' `
/ \
① 在home.test.com的根目录 ② 用mod_rewrite重写
设跳转文件,ASP代码举例: URL指向[username]目录。
| |
| |
| |
方法①纯代码方式,无用户目录。
<?php
.........
$pre_host=str_replace('.'.$db_domain,'',$_SERVER['HTTP_HOST']);
if(!$db_userdomain || strpos($db_blogurl,$_SERVER['HTTP_HOST'])!==false || gethostbyname($_SERVER['HTTP_HOST'])=='127.0.0.1'){
…………
} else{
.........
$pre_host=str_replace('.'.$db_domain,'',$_SERVER['HTTP_HOST']);
if(!$db_userdomain || strpos($db_blogurl,$_SERVER['HTTP_HOST'])!==false || gethostbyname($_SERVER['HTTP_HOST'])=='127.0.0.1'){
…………
} else{
$rt=$db->get_one("SELECT uid FROM my_table WHERE username='$pre_host'");
if($rt['uid']){
refreshto(http://$pre_host.$db_domain/$rt[uid]/index.html);
} else{
…………
}
}
…………
方法②用mod_rewrite重写URL,指向用户目录。
在blog的主目录下建立.htaccess文件,内容为:
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteRule ^(.*)/home/(.*)$ $1.php?$2
</IfModule>
方法②用mod_rewrite重写URL,指向用户目录。
在blog的主目录下建立.htaccess文件,内容为:
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteRule ^(.*)/home/(.*)$ $1.php?$2
</IfModule>