Nazmul
Nazmul

Reputation: 7218

How do I get root url using ASP not ASP.net

How do I get root url using ASP not ASP.net? I have found this question ( How do I get the site root URL? )

but it is related to ASP.net.

=====================================

Abbas's answer provide me the

parent site root url

but does not provide me the subsite root url

=====================================

Upvotes: 6

Views: 12126

Answers (3)

user1945782
user1945782

Reputation:

Though this is a very old question, I suspect what Hoque is asking for is everything before the page. For instance, if the URL is http://www.example.com/subsite/default.asp?a=1&b=2, the expected return value would be http://www.example.com/subsite.

Function GetSitePath()
    Dim address, port, url
    address = "http://"
    If LCase(Request.ServerVariables("HTTPS")) = "off" Then address = "https://"
    address = address & Request.ServerVariables("SERVER_NAME")
    port = Request.ServerVariables("SERVER_PORT")
    If port <> 80 And port <> 443 Then address = address & ":" & port
    url = Request.ServerVariables("URL")
    address = address & Left(url, InstrRev(url, "/"))
    GetSitePath = address
End Function

Note that I've included no error checking here, to prevent bad URLs, but if this causes an error I would suspect there's something a little more desperate going on!

Upvotes: 0

Robert
Robert

Reputation: 3074

This should get you what you want.

getSiteURL()

Function getSiteURL()

    dim port
    dim https 
    dim domainname
    dim filename
    dim querystring
    dim fullpath
    dim url

    port = "http" 
    https = lcase(request.ServerVariables("HTTPS")) 
    if https <> "off" then port = "https" 
    domainname = Request.ServerVariables("SERVER_NAME") 
    filename = Request.ServerVariables("SCRIPT_NAME") 
    querystring = Request.ServerVariables("QUERY_STRING") 
    fullpath = port & "://" & domainname & Request.ServerVariables("SCRIPT_NAME")
    filename = right(fullpath, InStr(StrReverse(fullpath), StrReverse("/")))

    url = Replace(fullpath, filename, "/")

    response.write url & "<br>" 
end Function 

Upvotes: 1

Abbas
Abbas

Reputation: 6886

Classic ASP had a Request.ServerVariables collection that contained all server and environment details. Here's what the classic ASP version of the example .NET code looks like:

function getSiteRootUrl()
    dim siteRootUrl, protocol, hostname, port

    if Request.ServerVariables("HTTPS") = "off" then
        protocol = "http"
    else
        protocol = "https"
    end if
    siteRootUrl = protocol & "://"

    hostname = Request.ServerVariables("HTTP_HOST")
    siteRootUrl = siteRootUrl & hostname        

    port = Request.ServerVariables("SERVER_PORT")
    if port <> 80 and port <> 443 then
        siteRootUrl = siteRootUrl & ":" & port
    end if

    getSiteRootUrl = siteRootUrl
end function

Upvotes: 13

Related Questions