[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

ASP.net 301 redirect code.

Ok, I have the following code to make sure that www.webspider.com is displayed all the time versuse webspider.com.
        Dim domain_Name, theURL, QUERY_STRING, HTTP_PATH
        domain_Name = LCase(Request.ServerVariables("HTTP_HOST")).ToString
        If domain_Name <> "www.webspider.com" Then
            HTTP_PATH = Request.ServerVariables("PATH_INFO")
            If Left(HTTP_PATH, 8) = "Default.aspx" Then
                HTTP_PATH = ""
            End If
            QUERY_STRING = Request.ServerVariables("QUERY_STRING")
            theURL = "http://www.webspider.com" & HTTP_PATH
            If Len(QUERY_STRING) > 0 Then
                theURL = theURL & "?" & QUERY_STRING
            End If
            Response.Clear()
            Response.Status = "301 Moved Permanently"
            Response.AddHeader("Location", theURL)
            Response.Flush()
            Response.End()
        End If
The problem is, when I'm testing new code and there's the "localhost" header it tries to redirect to the website. Yes, I can comment it out but having to do this every time I modify my code is a pain. Anyone know how to properly add "localhost" to the options? I've tried several ways with no success.
0
jumpstart0321
Asked:
jumpstart0321
1 Solution
 
Rajar AhmedConsultantCommented:
this can be done by writing seperately for localhost also

Dim domain_Name, theURL, QUERY_STRING, HTTP_PATH
        domain_Name = LCase(Request.ServerVariables("HTTP_HOST")).ToString 
If domain_Name = "localhost" Then
            HTTP_PATH = Request.ServerVariables("PATH_INFO")
            If Left(HTTP_PATH, 8) = "Default.aspx" Then
                HTTP_PATH = ""
            End If
            QUERY_STRING = Request.ServerVariables("QUERY_STRING")
            theURL = "http://www.webspider.com" & HTTP_PATH
            If Len(QUERY_STRING) > 0 Then
                theURL = theURL & "?" & QUERY_STRING
            End If
            Response.Clear()
            Response.Status = "301 Moved Permanently"
            Response.AddHeader("Location", theURL)
            Response.Flush()
            Response.End()
        End If
else if domain_Name  <> "www.webspider.com" Then
 HTTP_PATH = Request.ServerVariables("PATH_INFO")
            If Left(HTTP_PATH, 8) = "Default.aspx" Then
                HTTP_PATH = ""
            End If
            QUERY_STRING = Request.ServerVariables("QUERY_STRING")
            theURL = "http://www.webspider.com" & HTTP_PATH
            If Len(QUERY_STRING) > 0 Then
                theURL = theURL & "?" & QUERY_STRING
            End If
            Response.Clear()
            Response.Status = "301 Moved Permanently"
            Response.AddHeader("Location", theURL)
            Response.Flush()
            Response.End()
        End If
end if

Open in new window

0
 
aplusexpertCommented:
Please use Mode condition


Like in debug write code for localhost and in Release mode write code for actual path


like

#if DEBUG
theURL = "http://localhost" & HTTP_PATH
#else
theURL = "http://www.webspider.com" & HTTP_PATH

0
 
jumpstart0321Author Commented:
Here's what worked. I simply created a condition to test for localhost and only if it's not found test webspider. Sort of similar to the first answer:
Dim domain_Name, theURL, QUERY_STRING, HTTP_PATH
        domain_Name = LCase(Request.ServerVariables("HTTP_HOST")).ToString
If domain_Name <> "localhost" Then
else if domain_Name  <> "www.webspider.com" Then
 HTTP_PATH = Request.ServerVariables("PATH_INFO")
            If Left(HTTP_PATH, 8) = "Default.aspx" Then
                HTTP_PATH = ""
            End If
            QUERY_STRING = Request.ServerVariables("QUERY_STRING")
            theURL = "http://www.webspider.com" & HTTP_PATH
            If Len(QUERY_STRING) > 0 Then
                theURL = theURL & "?" & QUERY_STRING
            End If
            Response.Clear()
            Response.Status = "301 Moved Permanently"
            Response.AddHeader("Location", theURL)
            Response.Flush()
            Response.End()
        End If
end if
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now