2010/03/13

使用Windows Server 達成自動次域名的建置

環境:
Windows Server 2003 DNS *1 + IIS*1 分別不同二台主機

用途:新增次域名,提供合作夥伴網站
域名假設是:domain.com.tw
次域名:sub.domain.com.tw
實際路徑為:http://www.domain.com.tw/sub

sub讓廠商自由取名
網站行銷問題:打算直接用搜尋,或是http://sub.domain.com.tw
網站行銷,有錢、沒錢,作法差別很大,不贅述。

注意:DNS關係,最遲可能24小時才會生效。

有二個方式:直接增加DNS紀錄並調整IIS、ISAPI_Rewrite

方式一,直接增加DNS紀錄並調整IIS,注意順序
1.首先確認主網域DNS紀錄
*.example.com IN A 1.2.3.4

2.手動輸入次域名
sub1.example.com IN A 1.2.3.4
sub2.example.com IN A 1.2.3.4
sub3.example.com IN A 1.2.3.4

3.設定IIS目錄
d:\inetpub\wwwroot\example.com\sub1\
d:\inetpub\wwwroot\example.com\sub2\
d:\inetpub\wwwroot\example.com\sub3\

4.為每個次網域建立網站,每一個都是一樣步驟
  • Open IIS Management Console.
  • Click on the Web Sites folder and select New : Web Site.
  • Click on Next to continue.
  • Enter the description for your site then click Next. An example would be: sub1.example.com.
  • On the IP Address and Port Setting, enter sub1.example.com into the Host header for this Web site field.
  • On the next page, enter the path d:\inetpub\wwwroot\example.com\sub1\.
  • On the next page, select your options click Next and you're done.

5.某些共用元件(javascript, style sheets, and images)的處理:

  • Right click on the subdomain you have just created in IIS Management Console and select New : Virtual Directory.
  • As an example we'll share the style sheet folder (css). Click on Next and enter css in the Alias field.
  • Enter the path d:\inetpub\wwwroot\example.com\css\ into the Path field and click on Next.
  • Specify the permission on the next page and you're done.

     

    方式二、使用ISAPI_Rewrite
    1.同方式一,首先確認主網域DNS紀錄
    *.example.com IN A 1.2.3.4

    2.確認主網站基礎狀態:

  • Open IIS Management Console and select your web site.
  • Right click on it and select Properties.
  • Click on Web Site tab.
  • Click on Advanced button.
  • Make sure there is one entry under the Multiple identities for this Web Site with Host Header Name field blank. This entry will intercept all requests that comes to this IP address.
  • Make sure the IP address is only used by this web site.

    3.設定httpd.ini 給 ISAPI_Rewrite 使用
          httpd.ini 內容包含下列,並確認置於網站根目錄下

       1: # Convert http://example.com to http://www.example.com/
       2: RewriteCond Host: ^example.com
       3: RewriteRule (.*) http\://www\.example.com$1 [I,RP]
       4:  
       5: # Assuming we have limited number of shared folders.
       6: # We will execute them accordingly regardless of the subdomain.
       7: # Example: http://sub1.example.com/img/logo.jpg -> /img/logo.jpg
       8: # Example: http://www.example.com/img/logo.jpg -> /img/logo.jpg
       9: RewriteRule (/css/.*) $1 [I,O,L]
      10: RewriteRule (/js/.*) $1 [I,O,L]
      11: RewriteRule (/img/.*) $1 [I,O,L]
      12:  
      13: #Redirect all other subdirectories not matching
      14: #to the list above as subdomains
      15: #example: www.example.com\sub1 -> sub1.example.com
      16: RewriteCond Host: www\.example\.com
      17: RewriteRule /(\w*)/(.*) http\://$1\.example\.com$2 [I,RP]
      18:  
      19: # If the web site starts with www then point the file to the root folder
      20: # If you specifically created a folder /www/ then you can comment out this section.
      21: RewriteCond Host: (?:www\.)example.com
      22: RewriteRule (.*) $1 [I,O,L]
      23:  
      24: # Any web site starts other than www will be re-mapped to /<subdomain>/
      25: # Example: http://sub1.example.com/default.asp -> /sub1/default.asp
      26: # Note: if the folder does not exists, then the user will get a 404 error automatically.
      27: RewriteCond Host: (.*)\.example.com
      28: RewriteRule (.*) /$1$2 [I,O,L]
      29:  
      30: #Fix missing slash char on folders
      31: #This has to be at the end because if invalid dir exists,
      32: #we should show 404 first
      33: RewriteCond Host: (.*)
      34: RewriteRule ([^.?]+[^.?/]) http\://$1$2/ [I,RP]
      35:  

    第二個方式快速得多,只要建立一次,詳細資訊請見[原文]


  • 沒有留言:

    張貼留言