Link to home
Start Free TrialLog in
Avatar of rrhandle8
rrhandle8Flag for United States of America

asked on

ASP.NET SEO Redirect

I need to write a SEO redirect for my site.

The live site is www.ticketstoregame.com
The staging site is www.ticketstoregame.net

Their are build sites for both on my pc.

I do not want to write separate global.asxa pages for each site. Just one that will handle all the possibilities.

You can see in the attached sub from the global.asxa file, I am tyring to prevent the redirect when I am on the .net site, but it redirects me to the .com site.

What am I doing wrong?




 Protected Sub Application_BeginRequest(ByVal sender As [Object], ByVal e As EventArgs)
       
        'Declare the server URL ex:www.mysite.com
        Dim server As String = Request.ServerVariables("SERVER_NAME")
       
       
        If server.Contains(".net") Then
            'Don't do anything
        Else
            'Declare the form being accessed ex: Default.aspx
            Dim url As String = Request.ServerVariables("URL")
            ' Declare the query string in the URL
            Dim querystring As String = Request.ServerVariables("QUERY_STRING")
            ' Merge the server name with the form ex: www.mysite.com/Default.aspx
            Dim fullurl As String = server & url
            ' Create a string for any URL with a querystring ex: ?&categoryid=1
            Dim tail As String = "?" & querystring
            ' Declare string you want replaced ex: www.
            Dim patternwww As String = "www."
            ' Declare what you want it replaced with
            Dim patternclear As String = String.Empty
            ' Create an if statement for URL's containing the string you dont want and containg query strings.
            If fullurl.Contains(patternwww) And querystring <> Nothing Then
                ' Replace www. with nothing
                Dim wwwrpl As String = fullurl.Replace(patternwww, patternclear)
                ' Build a string for the final URL
                Dim targeturl As String = "http://" & wwwrpl & tail
                ' Create the 301 Redirect
                Response.Clear()
                Response.Status = "301 Moved Permanently"
                Response.AddHeader("Location", targeturl)
                Response.[End]()
            End If
   
            ' Create an if statement for URL's containing the string you don't and don't contain query strings
            If fullurl.Contains(patternwww) And querystring = Nothing Then
                ' Replace www. with nothing
                Dim wwwrpl As String = fullurl.Replace(patternwww, patternclear)
                ' Build a string for the final URL
                Dim targeturl As String = "http://" & wwwrpl
                ' Create the 301 Redirect
                Response.Clear()
                Response.Status = "301 Moved Permanently"
                Response.AddHeader("Location", targeturl)
                Response.[End]()
            End If
    End Sub
SOLUTION
Avatar of Navneet Hegde
Navneet Hegde
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rrhandle8

ASKER

Both solutions worked.  There was something else in the code I forgot about.