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:
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.
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
If you have a better idea than what I've done here, please speak up.