4

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

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

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

谢谢

4

2 回答 2

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

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

于 2011-02-01T12:59:30.610 回答
5

试试这个:

Response.Status = "302 Moved Temporary"
Response.AddHeader "Location", "http://www.mywebsite.com/" 
于 2011-02-01T12:58:56.880 回答