经典 Asp 页面中的 URL 重定向

发布于 2024-10-15 12:40:16 字数 254 浏览 2 评论 0原文

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

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

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

谢谢

I have a issue related to redirection

I want to apply redirection if someone uses http://mywebsite.com/ then the URL would be redirected to http://www.mywebsite.com/. I know it is a 302 redirection but I dont know how to apply it in coding ........ what VBScript could be used for applying redirection ??

My website is build in Classic ASP and VBScript ... any piece of code would be better for me

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

动次打次papapa 2024-10-22 12:40:17

使用 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

上面执行重定向,确保保留请求的页面和查询字符串

Use Request.ServerVariables("HTTP_HOST") to get the host part so that you can check if it starts with www. or not.

If it doesn't then just issue a Response.Redirect() to the appropriate URL since it'll do a 302 for you:

e.g.

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

The above does a redirect ensuring the requested page and querystring are preserved

漫漫岁月 2024-10-22 12:40:17

试试这个:

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

Try this:

Response.Status = "302 Moved Temporary"
Response.AddHeader "Location", "http://www.mywebsite.com/" 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文