|  Login

Primary Portal Aliases / 301 Redirect Enhancement

Minimize

Since v1 of DotNetNuke portal aliases have been included to facilitate hosting multiple websites within a single installation of DotNetNuke. This was a great feature, but when you need multiple domains pointing to a single installation many users would run into issues with ranking in search engines (search engine optimization).

It's extremely hard for a search engine to determine the primary domain name when 301 redirects are not utilized. For the more advanced users, this was never a problem. You could easily setup another IIS host to handle the redirects, or get tricky with your DNS provider. However DotNetNuke should of always supported 301 redirects out of the box (but it doesn't) when multiple portal aliases are configured on a single portal. 

I've always maintained my own branch of DotNetNuke that I utilize in production for my clients, one of the features I've added into this branch is automatic 301 redirection based on the first portal alias in your portal instance. Basically, if more than one alias is listed, DNN will now automatically issue a 301 redirect to the first alias.

For DNN v5.1.2 you can download my modified HttpHandlers assembly, and test out the new enhancement on your development portals. 

If you're like me, and you don't install anything without first looking at the sourcecode than this page is for you. You'll want to locate the following section from the UrlRewiteModule.vb @ line #431


'using the DomainName above will find that alias that is the domainname portion of the Url
'ie. dotnetnuke.com will be found even if zzz.dotnetnuke.com was entered on the Url
objPortalAliasInfo = PortalAliasController.GetPortalAliasInfo(PortalAlias)
If Not objPortalAliasInfo Is Nothing Then
    PortalId = objPortalAliasInfo.PortalID
End If

and replace it with the following block of code


'using the DomainName above will find that alias that is the domainname portion of the Url
'ie. dotnetnuke.com will be found even if zzz.dotnetnuke.com was entered on the Url
objPortalAliasInfo = PortalAliasController.GetPortalAliasInfo(PortalAlias)
If Not objPortalAliasInfo Is Nothing Then
    PortalId = objPortalAliasInfo.PortalID

    Dim s_PortalAliasCacheKey As String = "OHpack" & PortalId
    'get the first alias assigned to the portal
    Dim arrPortalAliases As ArrayList = Common.Utilities.DataCache.GetCache(Of ArrayList)(s_PortalAliasCacheKey)

    'cache doesn't exist, hit the db and create the cache
    If arrPortalAliases Is Nothing Then
        arrPortalAliases = New PortalAliasController().GetPortalAliasArrayByPortalID(PortalId)
        Common.Utilities.DataCache.SetCache(s_PortalAliasCacheKey, arrPortalAliases)
    End If

    'only continue if we have more than 1 aliases configured on this portal
    If arrPortalAliases.Count > 1 Then

        'we want the first item in the array, that's the primary url/alias
        Dim objPrimaryPortalAliasInfo As PortalAliasInfo = CType(arrPortalAliases(0), PortalAliasInfo)

        'only continue if the aliases don't match
        If objPrimaryPortalAliasInfo.PortalAliasID <> objPortalAliasInfo.PortalAliasID Then

            'get the original url, dnn seems to replace every instance of the original url, so we have to retrieve it from the current requests cache
            Dim s_SEOedURL As String = Request.Url.AbsoluteUri
            If Not (HttpContext.Current.Items("UrlRewrite:OriginalUrl") Is Nothing) Then
                s_SEOedURL = HttpContext.Current.Items("UrlRewrite:OriginalUrl").ToString()
            End If

            'replace the current alias with the primary alias, this should leave the protocol / path+querystring intact
            s_SEOedURL = s_SEOedURL.Replace(objPortalAliasInfo.HTTPAlias.Replace("*.", ""), objPrimaryPortalAliasInfo.HTTPAlias.Replace("*.", ""))

            'process the redirect
            Response.StatusCode = 301
            Response.AppendHeader("Location", s_SEOedURL)
            Response.End()
        End If
    End If
End If

v5 - 2/2/2010 - 12:51 PM

  • DNN522 release

v4 - 12/17/2009 - 12:51 PM

  • DNN520 release

v3 - 9/17/2009 - 12:11 AM

  • fixed caching bug

v2 - 9/16/2009 - 11:18 PM

  • added caching
  • documented code

v1 - 9/16/2009 9:29 PM

  • initial release

 

If you access my website with the 'www.' prefix you will see the 301 redirection modification in action. You will have to watch the address bar in your browser closely as that prefix will instantly disappear. 



Any donation would be greatly appreciated and will go directly to the development of free extensions for the DotNetNuke community.

Downloads

Primary Portal Aliases
v0.0.5 - over 7 year ago
Displaying 1-7 of 29 items.
Loading...