简体   繁体   中英

Redirecting to www

I've added a global.asax to my web project, and I'm trying to check if the domain in the Begin_Request contains "www", and if it doesn't, then redirect them to the page they were requesting, but with the "www" pre-pended to it. For an example, If I try to access : SampleSite.com, it directs me to SampleSite.com/www.SampleSite.com/www.SampleSite.com/www.SampleSite.com/www.SampleSite.com/www.SampleSite.com/www.SampleSite.com/www.SampleSite.com/index.aspx

Here is the code:

<script runat="server">
    Sub Begin_Request(ByVal sender As Object, ByVal e As EventArgs) Handles Me.BeginRequest
        Dim domain As String = Request.Url.Host
        If (Not domain.StartsWith("www")) Then
            Dim path As String = ""
            path = String.Format("www.{0}", Request.Url.AbsoluteUri.Substring(7))
            Response.Redirect(path)
        End If

    End Sub
</script>

Any ideas?

I don't think you should do this by code but using IIS redirect features. Here you have an example about how to do it on IIS 7

In case you still want to stay with this approach, change

String.Format("www.{0}", Request.Url.AbsoluteUri.Substring(7));

by

String.Format("{0}://www.{1}", Request.Url.Scheme, Request.Url.AbsoluteUri.Substring(7));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM