代码之家  ›  专栏  ›  技术社区  ›  CountZero

将经典ASP请求路由到.NET-SEO重定向

  •  4
  • CountZero  · 技术社区  · 15 年前

    我们正在用.NET 3.5解决方案替换旧的经典ASP网站。

    我们需要将所有经典的ASP请求重定向到ASPX页面(即contact us.asp,现在可以路由到/contact us/default.aspx)。我想要的是请求访问global.asax,这样我可以做一些类似的事情

    If url == "bob.asp"
        Response.Status = "301 Moved Permanently";
        Response.AddHeader("Location", SiteConfig.SiteURL + redirectUrl);
    End If
    

    有两种不雅的解决方案。

    a)放置一个global.asa文件并通过该文件进行路由。

    b)将ASP文件映射到.NET引擎。很好,但是如果我们需要在我们的站点上托管经典的ASP站点,那么IIS会将请求发送到错误的位置。

    我在这里找到了一个很好的解决方案

    http://forums.asp.net/p/1202225/3458901.aspx

    这样说可能有用…

    <buildProviders>
    
    <add extension=".php" type="System.Web.Compilation.PageBuildProvider" />
    
    </buildProviders>
    <httpHandlers>
    
    <add verb="*" path="*.php" type="System.Web.UI.PageHandlerFactory" validate="True" />
    
    </httpHandlers>
    

    这个例子是针对PHP的,但我假设同样的东西也适用于ASP。但是,在示例中将.php更改为.asp,并将标记放在web.config的正确部分之后,我没有任何乐趣(实际上是500个服务器错误)。

    有人能解释一下这个问题吗?或者给我一个优雅的解决方案。

    有一种感觉,上面的解决方案对PHP或ASP不起作用,因为在将请求发送到.NET引擎之前,IIS已经将其路由。

    提前谢谢

    史蒂夫

    4 回复  |  直到 15 年前
        1
  •  4
  •   Eduardo Molteni    13 年前

    大编辑: @edsf在评论中指出答案是错误的。我不相信我用Firebug检查过,事实上, 这是错误的 .

    你需要使用 Context.Response.RedirectLocation 以便状态代码工作。

    enter image description here


    我在global.asax也这么做:

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim fullOriginalpath As String = Request.Url.ToString.ToLower
    
        If (fullOriginalpath.Contains("/verarticulo.asp?articuloid=")) Then
            Context.Response.StatusCode = 301
            ''// this does not work, returns a 302
            ''//Context.Response.Redirect("/noticias/" + getIDFromPath(fullOriginalpath))
    
            ''// this does right way
            Context.Response.RedirectLocation = "/noticias/" + getIDFromPath(fullOriginalpath)
            Context.Response.End()
        ElseIf (fullOriginalpath.Contains("/archivo.asp")) Then
            Context.Response.StatusCode = 301
            Context.Response.RedirectLocation = "/archivo/" 
            Context.Response.End()
        ElseIf (fullOriginalpath.EndsWith("/default.asp")) Then
            Context.Response.StatusCode = 301
            Context.Response.RedirectLocation = "/"
            Context.Response.End()
        End If
    End Sub
    

    如果您使用II6,那么只需配置这个ISAPI过滤器。 这样:

    enter image description here

    文件是 c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll

        2
  •  1
  •   x0n    15 年前

    最简单的方法是在IIS级别使用自定义404错误页。此页可以是任何ASPX页;您可以通过httpContext访问原始请求。

    在IIS6.0中,您会注意到默认情况下映射到.NET引擎(.aspx、.asmx等)的URL使用自己在web.config中定义的404处理程序。不管怎样,如果编辑ASPX引擎映射并确保设置了“文件必须存在”的勾选框,那么即使这些页也可以发送到IIS404页。这会将所有Boken链接重定向到IIS404处理程序,即使它们被映射到.NET处理程序。人们犯了一个错误:试图将所有断开的链接重定向到web.config定义的404handler;如果您只强制所有链接转到IIS定义的一个instread,那么就更容易了。

        3
  •  1
  •   Jeremy Mattingly    14 年前

    Eduardo Molteni的回答有效,除了一件事。它实际上通过了浏览器302而不是301。

    我相信,而不是:

    Context.Response.StatusCode = 301 
    Context.Response.Redirect("/something/") 
    

    应该是:

    Context.Response.StatusCode = 301 
    Context.Response.RedirectLocation = "/something") 
    

    response.redirect基本上会中断您使用response.statuscode设置的内容,浏览器最终会得到一个“302 found”。

    我不太清楚搜索引擎是如何处理302和301的,所以这可能不是问题。在我看来,一个真正的永久性重定向(301)将是首选。

    对于框架v4上的那些,似乎有一个新的选项:

    Response.RedirectPermanent("/something")
    

    我还没有测试过这个,但是我假设它提供了一个301作为状态代码。详情如下: HttpResponse.RedirectPermanent

        4
  •  0
  •   Mike J    15 年前

    我使用的是来自 http://evolvedcode.net/content/code_smart404/ . 我添加了代码来根据数据库中的表进行自定义映射。

    这个脚本可以很容易地在ASP.NET中重新编写,然后以相同的方式映射。