天天看點

ASP.NET 使用URLRewriter重寫二級域名

轉載自: http://www.cnblogs.com/zhhh/archive/2011/03/18/1987985.html

http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx?mfr=true

這裡要求對域名進行重寫,實作http://1234.abc.com/ 到 ~/Defa.aspx?id=1234的重寫。

第一:域名

首先域名要支援泛解悉,就是域名解悉的主機名為星号*,例:*.abc.com。如下圖

ASP.NET 使用URLRewriter重寫二級域名

這樣能保證你在浏覽器位址欄輸入任何字首,DNS都會把它們指向到你指定的IP位址上。

第二:IIS設定(Win2003 + IIS 6為例)

(1)網站必須為Web伺服器的預設站點,即端口号為80,主機頭為空的站點。如下圖所示。

ASP.NET 使用URLRewriter重寫二級域名

該站點接收所有對該伺服器的HTTP請求(其它設定為主機頭的站點除外)。是以任何二級域名通路該伺服器都會由該站點進行處理。

(2)另外要在站點的“通配符應用程式映射”清單中添加ASP.NET的Web請求處理程式aspnet_isapi.dll。如下圖所示。

ASP.NET 使用URLRewriter重寫二級域名

在這裡的設定,是讓該站點接到的所有請求都交給aspnet_isapi.dll處理。

第三:修改Microsoft的URLRewriter。

運作開源項目URLRewriter。這裡需要修改兩個地方:

(1)BaseModuleRewriter.cs類

view source print ?

01

protected

virtual

void

BaseModuleRewriter_AuthorizeRequest(

object

sender, EventArgs e)

02

03

{

04

05

HttpApplication app = (HttpApplication) sender;

06

07

//Rewrite(app.Request.Path, app);

08

09

Rewrite(app.Request.Url.AbsoluteUri, app);   

// ## ## ## 這裡修改了

10

11

}

這裡将app.Request.Path 替換成了 app.Request.Url.AbsoluteUri

(2)ModuleRewriter.cs類

view source print ?

01

protected

override

void

Rewrite(

string

requestedPath, System.Web.HttpApplication app)

02

03

{

04

05

// log information to the Trace object.

06

07

app.Context.Trace.Write(

"ModuleRewriter"

,

"Entering ModuleRewriter"

);

08

09

10

11

// get the configuration rules

12

13

RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;

14

15

16

17

// iterate through each rule...

18

19

for

(

int

i = 0; i < rules.Count; i++)

20

21

{

22

23

// get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)

24

25

//string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";

26

27

string

lookFor =

"^"

+ rules[i].LookFor +

"$"

;   

// ## ## ## 這裡修改了

28

29

30

31

// Create a regex (note that IgnoreCase is set...)

32

33

Regex re =

new

Regex(lookFor, RegexOptions.IgnoreCase);

34

35

36

37

// See if a match is found

38

39

if

(re.IsMatch(requestedPath))

40

41

{

42

43

// match found - do any replacement needed

44

45

string

sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));

46

47

48

49

// log rewriting information to the Trace object

50

51

app.Context.Trace.Write(

"ModuleRewriter"

,

"Rewriting URL to "

+ sendToUrl);

52

53

54

55

// Rewrite the URL

56

57

RewriterUtils.RewriteUrl(app.Context, sendToUrl);

58

59

break

;    

// exit the for loop

60

61

}

62

63

}

64

65

66

67

// Log information to the Trace object

68

69

app.Context.Trace.Write(

"ModuleRewriter"

,

"Exiting ModuleRewriter"

);

70

71

}

這裡将string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";

  改成了 string lookFor = "^" + rules[i].LookFor + "$";

這兩個地方修改完以後,生成項目。将項止目bin/Debug目錄下的URLRewriter.dll檔案Copy到我們要重寫URL的項目中。

第四:配置項目

(1)在web.config檔案的configSections節點下添加如下一行代碼

1 
     <
     section 
     name
     ="RewriterConfig"
      type
     ="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"
     />
          

這裡配置一個重寫配置的類

(2)修改httpModules節點,在裡面添加一行配置代碼

1 
     <
     add 
     type
     ="URLRewriter.ModuleRewriter, URLRewriter"
      name
     ="ModuleRewriter"
      
     />
          

(3)在主節點configuration節點下添加路由規則,代碼如下:

1 
         
     <!--
      URL重寫 将捕獲頁面轉發到實際位址  
     -->
     

      2 
     

      3 
         
     <
     RewriterConfig
     >
     

      4 
     

      5 
             
     <
     Rules
     >
     

      6 
     

      7 
                 
     <
     RewriterRule
     >
     

      8 
     

      9 
                     
     <
     LookFor
     >
     http://(/w+).abc.com/
     </
     LookFor
     >
     

     10 
     

     11 
                     
     <
     SendTo
     >
     ~/Defa.aspx?id=$1
     </
     SendTo
     >
     

     12 
     

     13 
                 
     </
     RewriterRule
     >
     

     14 
     

     15 
             
     </
     Rules
     >
     

     16 
     

     17 
         
     </
     RewriterConfig
     >
     

     18 
     

     19 
         
     <!--
      URL重寫 将捕獲頁面轉發到實際位址 ( 結束 )  
     -->
          

代碼裡一個RewriterRule節點就是一個規則,這時隻有一個,即把域名中的主機頭部分做為Defa.aspx頁面的id參數的值發送給Defa.aspx頁面。

注意:這裡LookFor節點裡的http://(/w+).abc.com/不能少了最後的反斜框

OK,一切完工

釋出,上傳到伺服器,測試一下,如圖

ASP.NET 使用URLRewriter重寫二級域名

繼續閱讀