troubleshooting Question

ASP.NET SEO Redirect

Avatar of rrhandle8
rrhandle8Flag for United States of America asked on
.NET ProgrammingASP.NET
4 Comments3 Solutions403 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
rrhandle8

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 3 Answers and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros