如何在 ASP.NET / IIS 6 中按模式自动重定向 URL?
我认为这应该是一个简单直接的问题,但在挣扎了太长时间之后,我将不得不寻求帮助。
我需要的是重定向与以下模式匹配的网络应用程序的所有请求 - “^(http://[^/]+/blogs/[^/.]+)/?$
”到“$1/Default.aspx
”的路径。
:http://myapp/blogs/randomdirthatdoesntexist
-> http://myapp/blogs/randomdirthatdoesntexist/default.aspx
)
(使用英语而不是正则表达式 “博客”中的目录实际上并不存在,而是由第 3 方产品处理对 randomdir/Default.aspx 的请求,但是当您转到“randomdir/”时,您会收到 404 not found,这就是我想要的 我尝试
使用 global.asax
和 HttpHandler
,但我无法启动对这些 404 路径的请求
。简而言之,我应该如何重定向不存在目录的路径?
谢谢。
I thought it should be a simple straight-forward matter but after struggling with it for too much time I'm gonna have to seek for help.
What I need is to redirect all requests for my web application that match the following pattern - "^(http://[^/]+/blogs/[^/.]+)/?$
" to the path of "$1/Default.aspx
".
(Using English rather than Regex: http://myapp/blogs/randomdirthatdoesntexist
-> http://myapp/blogs/randomdirthatdoesntexist/default.aspx
)
The sub-directories in "blogs" do not physically exist, instead a 3rd party product deals with the requests to randomdir/Default.aspx", but when you go to "randomdir/" you get 404 not found, which is what I'm trying to fix.
I tried to use global.asax
and also HttpHandler
, but I couldn't get either of them to fire up on the requests to those 404 paths.
So, in short, what should I do to redirect paths of non-existing directories?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过将 IIS 中的 404 错误页面设置为您创建的页面来完成类似的操作。该页面能够访问正在请求的页面,并执行一些附加逻辑。因此 randomdirthatdoesntexist/ 的 404 错误仍然会发生,但 404 处理程序注意到这一点并重定向到 randomdirthatdoesntexist/default.aspx。
我挖出了我的旧代码并对其进行了一些调整,以一种过于简化的方式完成您需要的操作,但我没有任何地方可以测试 IIS6:
I accomplished something similar to this by setting the Error Page for 404s in IIS to a page you create. This page is able to access the page which is being requested, and perform some additional logic. So the 404 for randomdirthatdoesntexist/ still happens, but the 404 handler notices this and redirects to randomdirthatdoesntexist/default.aspx.
I dug out my old code and tweaked it a little to do what you need in an over-simplified fashion, but I don't have IIS6 anywhere to test:
使用自定义 HttpModule,或者Http Handler,它位于 Http 请求管道的早期位置,可以在确定任何可能的 404 响应之前适当地引导您的请求。
请参阅我自己的 SO 问答; Server.Transfer 或 Server.RewritePath 了解更多详细信息。
Use a Custom HttpModule, or Http Handler, which sits in early enough in the Http Request pipeline to direct your requests appropriately before any possible 404 response is determined.
See my own SO Q&A; Server.Transfer or Server.RewritePath for more details.