繁体   English   中英

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

[英]IIS7 and permanent redirects using the location tag in web.config

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

<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个页面中的每个页面指定一个位置部分,但是希望有一个更简洁的方法。

您可以将.htm设置为由ASP.NET库处理(默认情况下与.aspx相同),然后在global.ascx中的Application_BeginRequest中查看请求,并在适当时重定向。

它比Web.config中的一两个重定向要复杂得多,但是经过一定数量后,它会变得更加容易。

将页面一页一页地输入到web.config中的一种替代方法是创建一个HttpModule IIS7 +处于集成模式时,默认情况下, HttpModules将针对所有内容(包括静态文件)运行。

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

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

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

此模块就像瑞士军刀,可以满足所有URL重定向或重写需求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM