12

I am working on classic asp application. I have use URL rewrite on some pages.

How can i get current url of the page in classic asp?

Example: http://www.site.com/page.asp ---> url rewrite in IIS ---> http://www.site.com/home/page

so here i want current url of the page which is http://www.site.com/home/page

Please help me. Thanks.

Maddy
  • 907
  • 3
  • 10
  • 25
  • Can't you read it with `Request.ServerVariables("server_name") & Request.ServerVariables("url")` ? Not sure if this works with rewritten urls too... – Fabian Lauer Mar 13 '13 at 20:02
  • no Request.ServerVariables("server_name") = www.site.com and Request.ServerVariables("url") = page.asp – Maddy Mar 13 '13 at 20:06
  • Could you please mark any of these answers if they were helpful for you? If not please let us know so that we can find the solution. – Konrad Mar 16 '13 at 01:49

3 Answers3

21

There's no fancy one function that does it all.

First you need to get the protocol (if it is not always http):

Dim protocol
Dim domainName
Dim fileName
Dim queryString
Dim url

protocol = "http" 
If lcase(request.ServerVariables("HTTPS"))<> "off" Then 
   protocol = "https" 
End If

Now the rest with optional query string:

domainName= Request.ServerVariables("SERVER_NAME") 
fileName= Request.ServerVariables("SCRIPT_NAME") 
queryString= Request.ServerVariables("QUERY_STRING")

url = protocol & "://" & domainName & fileName
If Len(queryString)<>0 Then
   url = url & "?" & queryString
End If

Hope it works for you.

Konrad
  • 1,014
  • 1
  • 13
  • 24
19

You can try to output all ServerVariables like so:

for each key in Request.Servervariables
  Response.Write key & " = " & Request.Servervariables(key) & "<br>"
next

Maybe the URL you seek is already there. We use the Rewrite module and there is a ServerVariable called HTTP_X_ORIGINAL_URL that contains the rewritten URL path, e.g. "/home/page" in your example.

Protocol (HTTPS=ON/OFF) and Server (SERVER_NAME) can also be found in the ServerVariables.

gpinkas
  • 2,291
  • 2
  • 33
  • 49
3

If you use URL Rewrite, the url data can only be retrieved in this way:

Request.ServerVariables("HTTP_X_ORIGINAL_URL")

Example

Dim domainName, urlParam
domainName = Request.ServerVariables("SERVER_NAME") 
urlParam   = Request.ServerVariables("HTTP_X_ORIGINAL_URL")
response.write(domainName & urlParam)