Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redirect to a URL based on the current URL 2

Status
Not open for further replies.

JunglesMcBeef

Programmer
Sep 18, 2003
266
Hey,

I'm pretty new to the whole web development side of things. Now, the problem is, i have the code, i just need a page property (the URL) to continue. Here is my code:

Code:
<script runat="server" language="VB">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim NewURL as String

    'This bit is where i don't know how to get the URL
    'eg
    'NewURL = some.thing.here.....

    If NewURL <> "mainURL" Then
	Response.Status = "301 Moved Permanently"
        Response.AddHeader("Location", "NewURL")
    End If
End Sub
</script>

What I am trying to achieve is a script that will redirect to different sites hosted on a domain based on the URL entered out of many possible URLs that resolve to the main hosting page. eg xxx.com.au entered as a URL will navigate to the zzzZzzz.com.au site, as will yyy.com.au. I want to redirect those URLs entered to the appropriate subfolder on the hosted site. hmm do i make any sense at all.
 
Try having a look at the Request.Url object which has various properties that contains this information.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Found the object almost immediately after posting haha, thx anyway. If you're interested, this is what I was trying to achieve - a simple redirection script for multiple sites hosted within subfolders of a domain (using aliases):

Code:
<script runat="server" language="VB">
Sub Page_Load(Sender as Object, e as EventArgs)
    Dim sURL as String
    Dim sPath as String
    Dim iURLLength as Integer

    sURL = Request.URL.ToString
    If InStr(8, sURL, "xyz12.com.au") = 0 Then
        iURLLength = Len(sURL)
        If InStr(6, sURL, "//[URL unfurl="true"]www.")[/URL] <> 0 Then
	   sPath = Mid(sURL, 12, (iURLLength -11))
        Else
	   sPath = Mid(sURL, 8, (iURLLength -7))
        End If	  
        Response.Redirect(sURL & "/" & sPath & "/index.htm")
    End If
End Sub
</script>
 
ok...ummm made it a bit more efficient:
Code:
<script runat="server" language="VB">
Sub Page_Load(Sender as Object, e as EventArgs)
    Dim sHost as String
    Dim sPath as String

    sHost = Request.URL.Host
    If sHost <> "xyz123.com.au" Then
        Response.Redirect("[URL unfurl="true"]http://"[/URL] & sHost & "/index.htm")
    End If
End Sub
</script>
 
Thanks for posting your solution


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Well, obviously my "improved" solution was flawed. So I thought I should post the script I ended up with, tested and complete:
Code:
<script runat="server" language="VB">
Sub Page_Load(Sender as Object, e as EventArgs)
    Dim sHost as String
    Dim sPath as String
    Dim iStart as Integer
    
    sPath = ""
    sHost = Request.URL.Host
    If InStr(sHost, "sq2.com.au") = 0 Then
        If Left(sHost, 4) = "[URL unfurl="true"]www."[/URL] Then
            iStart = 5
        Else
            iStart = 1
	End If
	Select Case Right(sHost, 7)
            Case ".com.au", ".net.au", ".org.au", ".edu.au", ".asn.au", _
              ".net.nz", ".org.nz"
                sPath = Mid(sHost, iStart, Len(sHost) - 7)
            Case Else
                Select Case Right(sHost, 6)
                    Case ".id.au", ".co.nz"
                        sPath = Mid(sHost, iStart, Len(sHost) - 6)
                    Case Else
                        Select Case Right(sHost, 5)
                            Case ".info"
                                sPath = Mid(sHost, iStart, Len(sHost) - 5)
                            Case Else
                                Select Case Right(sHost, 4)
                                    Case ".biz", ".com", ".net", ".org"
                                        sPath = Mid(sHost, iStart, Len(sHost) - 4)
                                End Select
                        End Select
                End Select
        End Select
	If sPath = "" Then
            Response.Redirect("[URL unfurl="true"]http://"[/URL] & sHost & "/index.htm")     
        Else  
            Response.Redirect("[URL unfurl="true"]http://"[/URL] & sHost & "/" & sPath & "/index.htm")
        End If
    End If
End Sub
</script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top