【问题标题】:Url Redirection in Classic Asp page经典 Asp 页面中的 URL 重定向
【发布时间】:2011-06-19 01:28:39
【问题描述】:

我有一个与重定向有关的问题

如果有人使用http://mywebsite.com/,我想应用重定向,那么 URL 将被重定向到 http://www.mywebsite.com/。我知道这是一个 302 重定向,但我不知道如何在编码中应用它............什么 VBScript 可以用于应用重定向??

我的网站是用 Classic ASP 和 VBScript 构建的……任何一段代码对我来说都会更好

谢谢

【问题讨论】:

    标签: asp-classic redirect http-status-code-302


    【解决方案1】:

    使用Request.ServerVariables("HTTP_HOST")获取主机部分,以便检查它是否以www.开头。

    如果没有,那么只需向相应的 URL 发出 Response.Redirect(),因为它会为您执行 302:

    例如

    If Left(Request.ServerVariables("HTTP_HOST"), 4) <> "www." Then
      Dim newUri
      'Build the redirect URI by prepending http://www. to the actual HTTP_HOST
      'and adding in the URL (i.e. the page the user requested)
      newUri = "http://www." & Request.ServerVariables("HTTP_HOST") & Request.ServerVariables("URL")
    
      'If there were any Querystring arguments pass them through as well
      If Request.ServerVariables("QUERY_STRING") <> "" Then
        newUri = newUri & "?" & Request.ServerVariables("QUERY_STRING")
      End If
    
      'Finally make the redirect
      Response.Redirect(newUri)
    End If
    

    上面做了一个重定向,确保请求的页面和查询字符串被保留

    【讨论】:

    • 使用来自 RobV 的代码和我在答案中发布的 302 重定向将是完整的解决方案。
    • @Randam Response.Redirect() 无论如何都会执行 302,因此无需手动设置状态代码和标题 - 请参阅 msdn.microsoft.com/en-us/library/ms524309.aspx
    【解决方案2】:

    试试这个:

    Response.Status = "302 Moved Temporary"
    Response.AddHeader "Location", "http://www.mywebsite.com/" 
    

    【讨论】:

    • 但要应用此解决方案,是否需要像@Robv 那样应用一些检查...` If Left(Request.ServerVariables("HTTP_HOST"), 4) "www."然后 Response.Status = "302 Moved Temporary" Response.AddHeader "Location", "mywebsite.com" end if ` 和 @Robv 代码我也可以保留查询字符串 ...
    • 保留 QueryString 而不是 Path/ScriptName 有什么意义?
    猜你喜欢
    • 1970-01-01
    • 2013-01-31
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2014-05-25
    相关资源
    最近更新 更多