Vbscript 从当前URL删除默认文档

Vbscript 从当前URL删除默认文档,vbscript,asp-classic,Vbscript,Asp Classic,我使用以下代码获取当前页面的URL thispage ="http://" & Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL") & "?" & Request.Querystring 我想检查用户是否在URL末尾键入了默认文档(index.asp),并将其删除(通过重定向到地址栏中没有默认文档的干净URL) 但此代码始终包含默认文档,即使未在地址栏中键入,例如,当我在

我使用以下代码获取当前页面的URL

thispage ="http://" & Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL") & "?" & Request.Querystring
我想检查用户是否在URL末尾键入了默认文档(index.asp),并将其删除(通过重定向到地址栏中没有默认文档的干净URL)

但此代码始终包含默认文档,即使未在地址栏中键入,例如,当我在地址栏中输入时,上面的代码将返回


如何编辑上述代码以区分这些URL?

您可以执行以下操作:

url = Request.ServerVariables("URL")
url = Left( url, Len( url, Right( url, InStrRev( url, "/" ) - 1 )
thispage ="http://" & Request.ServerVariables("SERVER_NAME") & url & "/?" & Request.Querystring

在您不知道适用的默认文档是什么的环境中,这是一项复杂的任务,但我认为在您的情况下,它总是
index.asp

如果是这样的话,你可以用下面的方法来做

defaultFile = "/index.asp" ' leading slash is mandatory
reqUrl = Request.ServerVariables("URL")
reqQS = Request.ServerVariables("QUERY_STRING")

'put a leading question mark if there's a query
If reqQS <> "" Then
    reqQS = "?" & reqQS
End If

'check if URL ends with "/index.asp" (case-insensitive comparison should be made)
If StrComp(Right(reqUrl, Len(defaultFile)), defaultFile, vbTextCompare) = 0 Then
    ' remove from reqUrl by preserving leading slash
    reqUrl = Left(reqUrl, Len(reqUrl) - Len(defaultFile) + 1)
End If

thispage = "http://" & Request.ServerVariables("SERVER_NAME") & reqUrl & reqQS
defaultFile=“/index.asp”必须使用斜杠开头
reqUrl=Request.ServerVariables(“URL”)
reqQS=Request.ServerVariables(“查询字符串”)
'如果有疑问,请在前面打一个问号
如果要求“”,则
reqQS=“?”&reqQS
如果结束
'检查URL是否以“/index.asp”结尾(应进行不区分大小写的比较)
如果StrComp(右(reqUrl,Len(defaultFile)),defaultFile,vbTextCompare)=0,则
'通过保留前导斜杠从reqUrl中删除
reqUrl=Left(reqUrl,Len(reqUrl)-Len(defaultFile)+1)
如果结束
thispage=“http://”&Request.ServerVariables(“服务器名称”)&reqUrl&reqq
FYI中的代码仅删除脚本名称,而不仅仅是在
index.asp
时。如果将其用作共享代码,则会出现问题。