首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >经典Asp页面中的Url重定向

问经典Asp页面中的Url重定向
EN

Stack Overflow用户
提问于 2011-02-01 13:39:57
回答 2查看 15.1K关注 0票数 4

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

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

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

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-02-01 20:59:31

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

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

例如:

代码语言:javascript
复制
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

上面的代码进行了重定向,以确保请求的页面和查询字符串得到保留

票数 12
EN

Stack Overflow用户

发布于 2011-02-01 20:58:57

试试这个:

代码语言:javascript
复制
Response.Status = "302 Moved Temporary"
Response.AddHeader "Location", "http://www.mywebsite.com/" 
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4859316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档