开发者

How to re-write url in asp.net 3.5

开发者 https://www.devze.com 2023-01-25 01:48 出处:网络
I converted a project from html into aspx Issue is, all the extension got changed. e.g. \"www.example.com\\index.html\" Changed to \"www.example.com\\index.aspx\"

I converted a project from html into aspx

Issue is, all the extension got changed.

e.g. "www.example.com\index.html" Changed to "www.example.com\index.aspx"

which give problem to SEO's.

so now when i search for the web i get the link as www.example.com\index.html and if i try to go in it, it give me the error of 404 file not found.

I tried couple of methods for URL-ReWriting, it works fine at local side, but it fails at server side.

Both i tried in Global.asax

1.

Protected Overloads Sub Application_BeginRequest(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim CurrentPath As String = Request.Path.ToLower()
        Dim strPageName As String = CurrentPath.Substring(CurrentPath.LastIndexOf("/") + 1, (CurrentPath.LastIndexOf(".") - CurrentPath.LastIndexOf("/")) + 4)

        If strPageName.EndsWith(".html") Then
            Select Case strPageName
                Case "index.html"
                    RewriteUrl(CurrentPath)
            End Select
        End If
End Sub

Protected Sub RewriteUrl(ByVal URL As String)
    Dim CurrentPath As String = URL
    CurrentPath = CurrentPath.Replace(".html", ".aspx")
    Dim MyContext As HttpContext = HttpContext.Current
    MyContext.RewritePath(CurrentPath)
End Sub

2.

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    RegisterRoutes(RouteTable.Routes)
End Sub

Shared Sub Re开发者_如何学编程gisterRoutes(ByVal routes As RouteCollection)
    routes.Add("Index", New Route _
    ( _
       "index.html", New CustomRouteHandler("~/index.aspx") _
    ))
End Sub


Do you have control of the web server? What you most likely need SEO-wise is to create a permanent (301) URL redirect which you can do at the web server level...often in the server settings, or in a script file.

See: http://en.wikipedia.org/wiki/URL_redirection

I realize this doesn't answer your question directly, but if all you are trying to do is get the search results to the correct new page, this is the best way to do it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号