简体   繁体   English

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

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

I need to setup some 301 permanent redirects in the web.config of an ASP.NET application running under IIS7. 我需要在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>

All the pages I redirect from will redirect to the home page - is there a quicker and/or easier way of doing this as I have over 10 pages I need to redirect to default.aspx? 我重定向到的所有页面都将重定向到主页-是否有一种更快和/或更容易的方法,因为我有10多个页面需要重定向到default.aspx? I can specify a location section for each of the 10 pages but had hoped for a more concise approach. 我可以为10个页面中的每个页面指定一个位置部分,但是希望有一个更简洁的方法。

You could set .htm to be handled by the ASP.NET library (the same as .aspx is by default), and then look at the requests in Application_BeginRequest in your global.ascx, redirecting when appropriate. 您可以将.htm设置为由ASP.NET库处理(默认情况下与.aspx相同),然后在global.ascx中的Application_BeginRequest中查看请求,并在适当时重定向。

It's a bit more involved than one or two redirects in a Web.config, but after a certain amount it becomes easier. 它比Web.config中的一两个重定向要复杂得多,但是经过一定数量后,它会变得更加容易。

An alternative to entering your pages one-by-one into web.config is to create an HttpModule . 将页面一页一页地输入到web.config中的一种替代方法是创建一个HttpModule With IIS7+ in Integrated mode, by default HttpModules will run for all content, including static files. IIS7 +处于集成模式时,默认情况下, HttpModules将针对所有内容(包括静态文件)运行。

To do the redirect in code, call HttpResponse.RedirectPermanent(string url, bool endResponse) in an early event. 要在代码中进行重定向,请在早期事件中调用HttpResponse.RedirectPermanent(string url, bool endResponse) For best performance, set endResponse = true. 为了获得最佳性能,请设置endResponse = true。

I suggest that you use the IIS URL Rewrite module. 我建议您使用IIS URL重写模块。

It's an official module that you can download here: http://www.iis.net/download/urlrewrite 它是一个官方模块,您可以在这里下载: http : //www.iis.net/download/urlrewrite

This module is like a swiss army knife for all URL redirection or rewriting needs. 此模块就像瑞士军刀,可以满足所有URL重定向或重写需求。

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

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