【问题标题】:removing default document from current URL从当前 URL 中删除默认文档
【发布时间】:2017-06-06 22:07:03
【问题描述】:

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

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

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

但即使没有在地址栏中键入,此代码也始终包含默认文档,例如当地址栏中有http://example.com 时,上面的代码返回http://example.com/index.asp

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

【问题讨论】:

  • 仅供参考,accepted answer 中的代码只是删除脚本名称,而不仅仅是在 index.asp 时。如果你把它用作共享代码,那就有问题了。

标签: vbscript asp-classic


【解决方案1】:

在您不知道适用的默认文档是什么的环境中,这是一项复杂的任务,但我认为您的情况总是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

【讨论】:

    【解决方案2】:

    你可以这样做:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      • 2019-07-13
      • 1970-01-01
      • 2014-11-06
      • 2014-04-22
      • 1970-01-01
      相关资源
      最近更新 更多