泛域名與web服務的mod_rewrite

 目的:動態解析home.sst.cn的下級域名,username.home.sst.cn,實現基於虛擬域名(動態域名)的個人空間訪問 。

實現步驟:

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


以上是在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%>


   b) 如果是Apache,則修改httpd.conf文件加入虛擬主機(如果需要同時供應多個web服務)
  
<VirtualHost *:80>
         ServerAdmin [email protected]        
         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

  分析訪問過程:
    用戶輸入[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{

       $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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章