使用web.config中的location标签进行IIS7和永久重定向

本文关键字:IIS7 重定向 config web 中的 location 标签 使用 | 更新日期: 2023-09-27 18:17:49

我需要在web中设置一些301永久重定向。配置一个ASP。. NET应用程序在IIS7下运行。

<configuration>
  <location path="services.htm">
    <system.webServer>
      <httpRedirect enabled="true" destination="default.aspx" httpResponseStatus="Permanent" />
    </system.webServer>
  </location>
  <location path="products.htm">
    <system.webServer>
      <httpRedirect enabled="true" destination="default.aspx" httpResponseStatus="Permanent" />
    </system.webServer>
  </location>
</configuration>

我重定向的所有页面都将重定向到主页-是否有更快和/或更简单的方法来做到这一点,因为我有超过10个页面需要重定向到default.aspx?我可以为每一个10页指定一个位置部分,但希望有一个更简洁的方法。

使用web.config中的location标签进行IIS7和永久重定向

您可以将.htm设置为由ASP处理。. NET库(默认是.aspx),然后查看全局的Application_BeginRequest中的请求。

这比Web上的一两个重定向更复杂。

将您的页面逐一输入web的替代方法。config命令用于创建HttpModule。当IIS7+处于集成模式时,默认情况下HttpModules将运行所有内容,包括静态文件。

要在代码中重定向,请在早期事件中调用HttpResponse.RedirectPermanent(string url, bool endResponse)。为获得最佳性能,请设置endResponse = true。

我建议你使用IIS URL重写模块。

这是一个官方模块,你可以在这里下载:http://www.iis.net/download/urlrewrite

这个模块就像一把瑞士军刀,用于所有URL重定向或重写需求。