从当前 URL 中删除默认文档

removing default document from current URL

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

thispage ="http://" & Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL") & "?" & Request.Querystring

我想检查用户是否在 URL 末尾键入了默认文档 (index.asp) 并将其删除(通过重定向到没有默认文档的干净 URL地址栏)。

但此代码始终包含默认文档,即使未在地址栏中键入,例如地址栏中 returns http://example.com/index.asp when I have http://example.com 上方的代码。

如何编辑以上代码以区分那些 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