Link to home
Start Free TrialLog in
Avatar of jumpstart0321
jumpstart0321

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of Rajar Ahmed
Rajar Ahmed
Flag of India 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
Avatar of aplusexpert
aplusexpert

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

Avatar of jumpstart0321

ASKER

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