Asp classic 经典Asp页面中的Url重定向

Asp classic 经典Asp页面中的Url重定向,asp-classic,redirect,http-status-code-302,Asp Classic,Redirect,Http Status Code 302,我有一个关于重定向的问题 如果有人使用http://mywebsite.com/然后URL将被重定向到http://www.mywebsite.com/。我知道这是一个302重定向,但我不知道如何将其应用于编码。。。。。。。。什么VBScript可用于应用重定向 我的网站是建立在经典的ASP和VBScript。。。任何一段代码对我都会更好 谢谢试试这个: Response.Status = "302 Moved Temporary" Response.AddHeader "Location",

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

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

我的网站是建立在经典的ASP和VBScript。。。任何一段代码对我都会更好

谢谢

试试这个:

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

使用
Request.ServerVariables(“HTTP\U主机”)
获取主机部件,以便检查它是否以
www.
开头

如果没有,则只需发出一个
响应。重定向()
到相应的URL,因为它将为您执行302:

e、 g

如果离开(Request.ServerVariables(“HTTP_HOST”),4)“www.”那么
迪姆纽里
'通过前缀构建重定向URIhttp://www. 到实际的HTTP_主机
'并添加URL(即用户请求的页面)
纽乌里=”http://www.&Request.ServerVariables(“HTTP_主机”)&Request.ServerVariables(“URL”)
'如果有任何查询字符串参数,请同时传递它们
如果Request.ServerVariables(“查询字符串”),则
newUri=newUri&“?”&Request.ServerVariables(“查询字符串”)
如果结束
'最后进行重定向
重定向(newUri)
如果结束

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

使用RobV提供的代码和302重定向,我在回答中发布的302重定向将是完整的解决方案。@Randam
Response.redirect()执行302操作,因此无需手动设置状态代码和标题-请参阅,但要应用此解决方案,难道没有像@Robv那样应用一些检查吗…`如果离开(Request.ServerVariables(“HTTP_HOST”)、4)“www.”然后Response.Status=“302 Moved Temporary”Response.AddHeader“Location”、“end If`并且使用@Robv代码,我也可以保留QueryString……保留QueryString而不是路径/脚本名有什么意义?
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