6

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

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

Community
  • 1
  • 1
Nazmul
  • 7,078
  • 12
  • 51
  • 63
  • 1
    possible duplicate of [ResolveUrl/Url.Content Equivalent in Classic Asp](http://stackoverflow.com/questions/7963635/resolveurl-url-content-equivalent-in-classic-asp) – Kerrek SB Dec 24 '11 at 16:46

3 Answers3

13

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
Abbas
  • 6,720
  • 4
  • 35
  • 49
  • If my site url is http://parentSite/subsite/default.asp, will the above return http://parentSite/subsite as root url? – Nazmul Dec 22 '11 at 07:52
  • I have translated the .NET code to Classic ASP so it will do what the code in your example was doing. If your site is not using SSL and uses port 80, The code above will show: http:// parentSite/subsite. The best way to understand it is to run it and play around with it. It's quite simple and if you run into difficulties you can post them here. – Abbas Dec 22 '11 at 07:58
  • I have tested and it does not go to subsite – Nazmul Dec 22 '11 at 08:41
  • 1
    As I said, I translated the example. From your question, it's not clear what you require. There is not direct way to get what you need. Request.ServerVariables("URL") and Request.ServerVariables("PATH_INFO") contain the path of the script including subfolders. By parsing these variables, you should be able to get what you need. – Abbas Dec 22 '11 at 09:02
  • @Abbas: Its close, there is an assumption that HTTPS is _not_ being run through port 80 and HTTP is _not_ being run through port 443 (it would be mad to do that but technically it is possible). – AnthonyWJones Dec 22 '11 at 12:28
  • @Abbas: What I want is very clear. I want the root url of site. If it is not clear, I am going to give an example. I parked a site named "subSite" in http :// www.parentSite. Therefore the root of my new site will be http:// www.parentSite/subSite and I would like to get the address of the subsite. – Nazmul Dec 22 '11 at 12:37
  • Error in this: You do not need to add the port. Non-standard/explicit ports are already included in HTTP_HOST (at least in my testing). – feetwet Jan 19 '16 at 18:25
1

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 
Jonatas
  • 88
  • 1
  • 8
Robert
  • 3,074
  • 3
  • 24
  • 32
  • No, it does not. It provides the part of the url from where I have call the function. For example, if I call the function from this page parentSite/childsite/Folder/Default.asp it returns parentSite/childsite/Folder – Nazmul Dec 26 '11 at 01:09
  • so then I guess I'm not sure what you want? What I posted will return the path to the page your on. Is the function on a page different than you want to display and where is the page that is you will be getting the URL on? You may have to pass the information to the function in order for it to be used. – Robert Dec 26 '11 at 01:29
  • would like to return parentSite/childSite as the childsite's root url – Nazmul Dec 26 '11 at 01:31
  • 1
    if the page that will create the URL is under folder then you'll either need to parse out the URL you want or pass it to the function from the calling page. Using what I have posted you could parse it out if you know how far back up the chain you want. – Robert Dec 26 '11 at 01:55
  • As I need the root url of the childsite/parentsite, if it is programaticlly difficult, it might be better to keep it as a constant. By the way, @Abbas's solution returns the root url of parent site but not the childsite. – Nazmul Dec 26 '11 at 02:07
  • right you can get the root of the parent site using Request.ServerVariables("HTTP_HOST") but when you say child site IIS doesn't know what your trying to get as a childsite could mean almost anything depending on your site structure. So your best bet is to take the whole url and rebuild it in code by splitting Request.ServerVariables("URL") and the looping through the array. If you know your child is 1 level off the parent then you loop to position 1 in the array. – Robert Dec 26 '11 at 02:37
  • Like ASP.net, Classic ASP does not have ResolveUrl("~/") or any equivalent probably. – Nazmul Dec 26 '11 at 02:45
0

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!

Paul
  • 4,160
  • 3
  • 30
  • 56