We help IT Professionals succeed at work.

ASP.NET SEO Redirect

rrhandle8
rrhandle8 asked
on
400 Views
Last Modified: 2012-06-27
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
Comment
Watch Question

Navneet.Net Full Stack Developer
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Both solutions worked.  There was something else in the code I forgot about.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.