Friday, March 28, 2008
How to achieve 301 permanent redirection in Dotnetnuke?
By j sumant @ 8:19 PM :: 1213 Views :: 0 Comments :: :: Technical, Featured, Dotnetnuke, asp.net

The web site where we need to implement 301 redirection was developed on dotnetnuke framework 4.5. Dotnetnuke framework 4.8.x provides "RequestFilter" setting for different kind of redirections where one has to provide from url, to url and type of redirection (Permanent redirect, normal redirect etc.). But since we don't have sufficient time to upgrade an existing site from dnn 4.5 to dnn 4.8 version. So I found a way to tackle this situation in following manner.

We have a two options for handling this situation.

  1. First option: Developing a HTTPModule

    On Application_BeginRequest event I need to read one config file where set of urls are mentioned in following manner.
        <urls>
            <url>
                <FromUrl></FromUrl>
                <ToUrl></ToUrl>
            </url>
        </urls>
        and if requested url belongs to one of the <FromUrl> tag mentioned in the config file, then this module will redirect to respective url mentioned in respective <ToUrl> tag

    After building this solution, I was supposed to provide one assembly to implementation team along with web.config entry for defining this httpmodule. But developing and testing this solution was going to take some time. But this is the the CORRECT way.

  2. Second option

    But for the time being solving immediate problem is more important, hence I had provided them a quick and cheap solution. I asked implementation team to add following lines of code in Application_BeginRequest method of Global.asax.vb file. Building the solution and deploying changed assembly on the server was not at all required . Dynamic compilation feature of ASP.net 2.0 enabled me to change respective file on the server directly.

        Dim strRequestPath As String
        strRequestPath = Request.Url.AbsolutePath.ToLower()

        Select Case strRequestPath
            Case "/oldurlpath/index.aspx"
                Response.Status = "301  Moved Permanently"
                Response.AddHeader("Location", "/newurlpath/index.aspx")
            Case "/oldurlpath1/index.aspx"
                Response.Status = "301  Moved Permanently"
                Response.AddHeader("Location", "/newurlpath1/index.aspx")      
        End Select