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

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

1

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

7 Comments

If my site url is parentSite/subsite/default.asp, will the above return parentSite/subsite as root url?
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.
I have tested and it does not go to subsite
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: 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).
|
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 

7 Comments

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
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.
would like to return parentSite/childSite as the childsite's root url
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.
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.
|
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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.