简体   繁体   中英

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.

<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? I can specify a location section for each of the 10 pages but had hoped for a more concise approach.

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.

It's a bit more involved than one or two redirects in a Web.config, but after a certain amount it becomes easier.

An alternative to entering your pages one-by-one into web.config is to create an HttpModule . With IIS7+ in Integrated mode, by default HttpModules will run for all content, including static files.

To do the redirect in code, call HttpResponse.RedirectPermanent(string url, bool endResponse) in an early event. For best performance, set endResponse = true.

I suggest that you use the IIS URL Rewrite module.

It's an official module that you can download here: http://www.iis.net/download/urlrewrite

This module is like a swiss army knife for all URL redirection or rewriting needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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