经典Asp页面中的URL重定向

New*_*eak 4 redirect asp-classic http-status-code-302

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

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

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

谢谢

Rob*_*obV 12

使用Request.ServerVariables("HTTP_HOST")得到的主机部分,这样就可以检查它是否与启动www.与否.

如果没有,那么只需Response.Redirect()向相应的URL 发出一个,因为它会为你做一个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
Run Code Online (Sandbox Code Playgroud)

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


Ran*_*dam 5

尝试这个:

Response.Status = "302 Moved Temporary"
Response.AddHeader "Location", "http://www.mywebsite.com/" 
Run Code Online (Sandbox Code Playgroud)