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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine Virtual Path for Current Application/Site Root 1

Status
Not open for further replies.

ESquared

Programmer
Dec 23, 2003
6,129
US
This started out as a question, but I figured out an answer so here goes. If you have this problem, read down:

How does one determine the virtual path for the current application root (or site root if there is no application)?

I have a web project that on the test server is an application of the Default Web Site (no wasting IPs on a test bed) but on the production server is its own web site with its own IP. Because of the DNS management software we use, having two hostnames point to the same A record is problematic, so virtual hosting with the same IP is out on the test server.

I want to write the web site in such a way that when I push it to production after testing, I don't have to change anything. In fact, I would like the site to be fairly tolerant of how it gets set up: as a web site, as an application in a site, or even on a new server. So being able to automatically determine the root when I am presenting some URLs will be helpful.

I've searched the internet, scanned the FAQs in this forum, and played around with server variables, all with no luck.

I know that, for example, one can construct the full URL of the current page with careful combination of the following ServerVariables:

SERVER_PROTOCOL
SERVER_NAME
SERVER_PORT
SCRIPT_NAME

But these break at the wrong places for me to figure out what I need.

... But then I figured it out. Answer is here:

Code:
Dim AppRoot

AppRoot = Request.ServerVariables("SCRIPT_NAME")
AppRoot = Left(AppRoot, Len(AppRoot) - Len(Request.ServerVariables("PATH_TRANSLATED")) + Len(Request.ServerVariables("APPL_PHYSICAL_PATH")))
If Request.ServerVariables("SERVER_PORT") <> "80" Then AppRoot = ":" & Request.ServerVariables("SERVER_PORT") & AppRoot
AppRoot = "://" & Request.ServerVariables("SERVER_NAME") & AppRoot
If Request.ServerVariables("Https") = "on" Then AppRoot = "s" & AppRoot
AppRoot = Split(LCase(Request.ServerVariables("SERVER_PROTOCOL")), "/")(0) & AppRoot
AppRoot now contains a full URL in the form "http:/[white][/white]/servername/..." where ... is the path to the current application or site root (if any). Alter for your own needs.

If you have a better idea than what I've done here, please speak up.
 
Ah, the fine art of string parsing! Good work.
 
Thanks!

script_name has a portion that is part of the application root (if only "/"), and a portion that is part of some subdirectory of it. Since both filesystem & virtual are the same to the right of the root, you can use the difference between path_translated and the appl_physical_path to learn the amount necessary to chop off the end of it.

So it goes something like this. Your application root is at:

[tt][/tt]

Now, in a document reached by

[tt][/tt]

you want to learn the application root.

APPL_PHYSICAL_PATH: [blue]c:\inetpub\wwwroot\MyApplication\[/blue]
PATH_TRANSLATED: [blue]c:\inetpub\wwwroot\MyApplication\[/blue][green]path1\path2\file.asp[/green]
SCRIPT_NAME: /MyApplication/[green]path1/path2/file.asp[/green]

The blue sections together let you identify the green parts, which leave you the part in bold on the third line. All we need are lengths.

This DOES assume you don't have more than one nested level of virtual/redirected paths (what's the technical term for this)... that could probably be worked out if necessary, though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top