如何在 ASP.NET / IIS 6 中按模式自动重定向 URL?

发布于 2024-10-21 01:07:40 字数 569 浏览 5 评论 0原文

我认为这应该是一个简单直接的问题,但在挣扎了太长时间之后,我将不得不寻求帮助。

我需要的是重定向与以下模式匹配的网络应用程序的所有请求 - “^(http://[^/]+/blogs/[^/.]+)/?$ ”到“$1/Default.aspx”的路径。

http://myapp/blogs/randomdirthatdoesntexist -> http://myapp/blogs/randomdirthatdoesntexist/default.aspx

(使用英语而不是正则表达式 “博客”中的目录实际上并不存在,而是由第 3 方产品处理对 randomdir/Default.aspx 的请求,但是当您转到“randomdir/”时,您会收到 404 not found,这就是我想要的 我尝试

使用 global.asaxHttpHandler,但我无法启动对这些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青瓷清茶倾城歌 2024-10-28 01:07:40

我通过将 IIS 中的 404 错误页面设置为您创建的页面来完成类似的操作。该页面能够访问正在请求的页面,并执行一些附加逻辑。因此 randomdirthatdoesntexist/ 的 404 错误仍然会发生,但 404 处理程序注意到这一点并重定向到 randomdirthatdoesntexist/default.aspx。

我挖出了我的旧代码并对其进行了一些调整,以一种过于简化的方式完成您需要的操作,但我没有任何地方可以测试 IIS6:

protected void Page_Load(object sender, EventArgs e)
{
    String sQueryString = Request.RawUrl.Substring(Request.FilePath.Length);
    Regex rx = new Regex(@"^\?(?<errorcode>\d+);(?<args>.*)$", RegexOptions.ExplicitCapture | RegexOptions.Compiled);
    Match m = rx.Match(sQueryString);


    if (m.Success)
    {
        if (m.Groups["errorcode"].Value == "404")
        {
            String sArgs = m.Groups["args"].Value;
            Uri requestedUri = new Uri(sArgs);

            // You can now check the value of uri and perform redirection...
            if (requestedUri.ToString().EndsWith("/"))
            {
                Response.Redirect(requestedUri + "Default.aspx")
            }
        }
    }
    Response.StatusCode = 404;
}

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:

protected void Page_Load(object sender, EventArgs e)
{
    String sQueryString = Request.RawUrl.Substring(Request.FilePath.Length);
    Regex rx = new Regex(@"^\?(?<errorcode>\d+);(?<args>.*)$", RegexOptions.ExplicitCapture | RegexOptions.Compiled);
    Match m = rx.Match(sQueryString);


    if (m.Success)
    {
        if (m.Groups["errorcode"].Value == "404")
        {
            String sArgs = m.Groups["args"].Value;
            Uri requestedUri = new Uri(sArgs);

            // You can now check the value of uri and perform redirection...
            if (requestedUri.ToString().EndsWith("/"))
            {
                Response.Redirect(requestedUri + "Default.aspx")
            }
        }
    }
    Response.StatusCode = 404;
}
时光暖心i 2024-10-28 01:07:40

使用自定义 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文