【问题标题】:How to 301 redirect如何进行 301 重定向
【发布时间】:2010-12-21 10:56:42
【问题描述】:

好的,我需要知道如何进行重定向(在哪里放置代码或指定设置)。移动应用后,我们会从一个应用重定向到另一个应用。

例如,如果用户转到existing.example.com/archive/

我们希望将任何包含old.example.com 的请求重定向到new.example.com。 url 中的其余部分保持不变。例如/archive/ 就是一个例子,所以我们想将它们重定向到这个应用程序的新位置new.example.com/archive/

我需要弄清楚如何检查我们现有网站的传入 URL 是否有 existing.example.com,如果是,则仅用新的 new.example.com 替换该部分并保留 URL 中的其余内容

我知道您可以在 IIS 7 中或以编程方式执行此操作。我想我不明白在这两种情况下如何做到这一点。我安装了 IIS7 Rewrite 插件并且没问题,但这是我没有得到的:

图案:

重定向网址:

我看不到如何在该界面中匹配 existing.example.com,然后在 RedirectURL 中添加什么,因为我想将整个 URL 仅将 existing.example.com 更改为 new.example.com REdirectURL ...我不知道如何在 IIS 7 中执行此操作。

【问题讨论】:

标签: asp.net iis-7 redirect url-rewriting


【解决方案1】:

这里有一篇文章描述了如何使用 IIS7 URL 重写插件匹配一个域并重定向到另一个域:http://weblogs.asp.net/owscott/archive/2009/11/27/iis-url-rewrite-rewriting-non-www-to-www.aspx

【讨论】:

  • 谢谢,这是我一直在寻找的,因为我还不知道 IIS 插件的所有模板化参数。
【解决方案2】:

你不需要任何复杂的重写东西来做这样一个微不足道的重定向,这是一个基本的网络服务器功能。在 IIS7 中,您现在可以选择不安装它(从万维网服务->通用 HTTP 功能->HTTP 重定向),但这样做并不常见。

在 IIS 管理器中编辑主网站的“绑定”,使其仅响应“主机名:”new.example.com,然后创建一个绑定到主机名old.example.com 的新网站。对于这个站点,点击“HTTP Redirect”选项和“Redirect requests to this destination:”http://new.example.com/ with ‘Status code:’ 301.

在配置 XML 方面:

<system.webServer>
    <httpRedirect enabled="true" destination="http://new.example.com/" httpResponseStatus="Permanent" />
</system.webServer>

【讨论】:

【解决方案3】:

在您网站的 global.asax.cs 文件中,您可以从 BeginRequest 重定向,如下所示。您可以编写例程,根据需要使用 regex 或 string.replace() 或任何您喜欢的方式替换域名。

protected void Application_BeginRequest(Object sender, EventArgs e){ 
    ...parse urls... 
    Response.Redirect(myNewPath)
}

【讨论】:

  • 真正的约翰,根据设置的不同,他们可能需要更改一些处理程序才能使其正常工作。
  • -1。抱歉,Response.Redirect() 找到了 302,而不是 OP 中要求的 301。
【解决方案4】:

对于 asp.net 应用程序,我对 urlrewriting.net 有很好的体验,这是一个免费组件,您可以在 web.config 中配置重定向。它允许您输入正则表达式并指定带有反向引用的新 URL,例如像这样的东西(未经测试):

<add name="newdomain" virtualUrl="^http\://old.example.com/(.*)$"
  rewriteUrlParameter="ExcludeFromClientQueryString"
  destinationUrl="http://new.example.com/$1"
  redirect="Domain"
  redirectMode="Permanent"
  ignoreCase="true" /> 

缺点:这需要为每个 ASP.NET 应用程序配置(并且您可能需要重新配置 IIS 以通过 asp.net 路由所有内容,或者它可能仅适用于 .aspx 文件)。如果您想重定向您的 complete 域,最好在 IIS 级别而不是在应用程序级别执行此操作。但是,为此,您可能会在系统管理员所在的 http://serverfault.com 上获得更好的帮助...

【讨论】:

    猜你喜欢
    • 2018-07-30
    • 2014-05-27
    • 2021-09-11
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 2012-05-27
    相关资源
    最近更新 更多