Routing Classic ASP Requests To .NET - SEO Redirects

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 22:48:45

Big edit: I was pointed by @EdSF in the comments that the answer was wrong. In disbelief I checked using Firebug, and, in fact, it was wrong.

You need to use Context.Response.RedirectLocation for the status code to work.


I'm doing the same in 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

The only thing you have to do if you are using II6 you have to configure this ISAPI filter in this way:

The file is c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll

The simplest thing is to use a custom 404 error page at the IIS level. This page can be any ASPX page; you have access to the original request via the HttpContext.

In IIS 6.0, you'll notice that by default URLs mapped to the .NET engine (.aspx,,asmx etc) use their own 404 handler defined in web.config. No matter; you can cause even those pages to be sent to the IIS 404 page if you edit the ASPX engine mapping and ensure that the tickbox for "file must exist" is set. This redirects all boken links to the IIS 404 handler, even if they are mapped to .NET handlers. People make the mistake of trying to redirect all broken links to the web.config defined 404handler; it's must easier if you just force all to go to the IIS defined one instread.

Eduardo Molteni's answer works, except for one thing. It actually passes the browser a 302 instead of a 301.

I believe instead of:

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

it should be:

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

The response.redirect is basically interrupting what you were setting up with the Response.StatusCode and the browser ends up getting a "302 Found".

I'm not really sure how search engines handle a 302 vs a 301 so maybe its not an issue. It seems to me though that a true Permanent Redirect (301) would be preferred.

For those on v4 of the framework, there seems to be a new option:

Response.RedirectPermanent("/something")

I haven't tested this, but I'm assuming it provides a 301 as the status code. Details here: HttpResponse.RedirectPermanent

I use a modified version of the Smart 404 Handler script from http://evolvedcode.net/content/code_smart404/. I added code to do custom mapping based of a table in our database.

This script could easily be re-written in ASP.NET and then mapped the same way.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!