IIS7 和使用 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 个页面中的每一页指定一个位置部分,但希望有一种更简洁的方法。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将 .htm 设置为由 ASP.NET 库处理(默认情况下与 .aspx 相同),然后查看 global.ascx 中的 Application_BeginRequest 中的请求,并在适当时进行重定向。
它比 Web.config 中的一两个重定向要复杂一些,但在一定数量之后就会变得更容易。
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.
将页面逐个输入 web.config 的另一种方法是创建一个
HttpModule
。当 IIS7+ 处于集成模式时,默认情况下HttpModules
将为所有内容运行,包括静态文件。要在代码中执行重定向,请在早期事件中调用
HttpResponse.RedirectPermanent(string url, bool endResponse)
。为了获得最佳性能,请设置 endResponse = true。An alternative to entering your pages one-by-one into web.config is to create an
HttpModule
. With IIS7+ in Integrated mode, by defaultHttpModules
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.我建议您使用 IIS URL 重写模块。
这是一个官方模块,您可以在这里下载:
http://www.iis.net/download/urlrewrite
这个模块就像一把瑞士军刀满足所有 URL 重定向或重写需求。
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.