ASP.NET重定向到子域的请求(博客或www)

时间:2010-06-05 02:01:10

标签: asp.net url

将Web请求重定向到ASP.NET应用程序到子域的最简单方法是什么?

如果请求进入网址http://somesite.com/foo.aspx,则新的目标网址应为

  • http://www.somesite.com/foo.aspx
  • http://blog.somesite.com/foo.aspx

如何以编程方式将请求重定向到子域,保持URL的其余部分不变?

2 个答案:

答案 0 :(得分:3)

你可以试试l ike this 一种简单的方法,可以连接到Global.asax

这是我的班级

Imports System.Web.UI.HtmlControls        
Imports System.Web.UI        
Imports System.Web   

Public Class HelperPage  
    Inherits System.Web.UI.Page  

    ''# Force WWW     
    Public Shared Sub ForceWWW()  
        If Not GetServerDomain.StartsWith("www.") Then 
            HttpContext.Current.Response.Status = "301 Moved Permanently" 
            HttpContext.Current.Response.AddHeader("Location", "http://www." & GetServerDomain() & HttpContext.Current.Request.RawUrl)  
        End If 
    End Sub 

    Public Shared Function GetServerDomain() As String 
        Dim myURL As String = HttpContext.Current.Request.Url.ToString  
        Dim re As New Regex("^(?:(?:https?\:)?(?:\/\/)?)?([^\/]+)")  
        Dim m As Match = re.Match(myURL)  
        Return m.Groups(1).Value  
    End Function    
End Class 

这是我在Global.asax中的调用

Protected Sub Application_BeginRequest(ByVal sender As Object, ByVal e As System.EventArgs)  
            HelperPage.ForceWWW()  
End Sub 

使用那段代码..你将永远被迫转到你网站的WWW版本(假设IIS中的Host Header同时列出了www和非www版本。)

答案 1 :(得分:0)

根据您运行IIS的网站数量,您可以通过手动配置来执行此操作。网站 - 只需将所有组合添加到应用程序

或者你可以开发一个小的“通配符”重定向器。这是“服务器管理员问题”的“编程答案”:

只需让IIS上的默认网站处理每个“未知”请求的地址。 可以在“有效的域名列表”中查找进入的URL并进行适当的重定向。

我们的服务器上运行了这样的机制。

简易版本(简短的黑客)是检查请求的URL中是否有WWW,如果没有,则尝试将其添加到请求并将客户端重定向到该地址。

注意:这会使客户端执行serverroundtrip,从而产生额外的流量。所以,如果你有2000次点击pr。 minut和其中一半没有WWW,黑客可能不是最好的主意。

有用的回答?

相关问题