With the solution above the ashx files works, but aspx files not.
Here is a cleaner solution :
If PortalId <> -1 Then
' load the PortalSettings into current context
Dim _portalSettings As PortalSettings = New PortalSettings(TabId, objPortalAliasInfo)
app.Context.Items.Add("PortalSettings", _portalSettings)
' manage secure connections
If Request.Url.AbsolutePath.ToLower.EndsWith(".aspx") Then
' request is for a standard page
strURL = ""
' if SSL is enabled
If _portalSettings.SSLEnabled Then
' if page is secure and connection is not secure
If _portalSettings.ActiveTab.IsSecure = True And Request.IsSecureConnection = False Then
' switch to secure connection
strURL = requestedPath.Replace("http://", "https://")
strURL = formatDomain(strURL, _portalSettings.STDURL, _portalSettings.SSLURL)
End If
End If
' if SSL is enforced
If _portalSettings.SSLEnforced Then
' if page is not secure and connection is secure
If _portalSettings.ActiveTab.IsSecure = False And Request.IsSecureConnection = True Then
' check if connection has already been forced to secure
If Request.QueryString("ssl") Is Nothing Then
' switch to unsecure connection
strURL = requestedPath.Replace("https://", "http://")
strURL = formatDomain(strURL, _portalSettings.SSLURL, _portalSettings.STDURL)
End If
End If
End If
' if a protocol switch is necessary
If strURL <> "" Then
If strURL.ToLower.StartsWith("https://") Then
' redirect to secure connection
Response.Redirect(strURL, True)
Else ' when switching to an unsecure page, use a clientside redirector to avoid the browser security warning
Response.Clear()
' add a refresh header to the response
Response.AddHeader("Refresh", "0;URL=" & strURL)
' add the clientside javascript redirection script
Response.Write("<html><head><title></title>")
Response.Write("<!-- <script language=""javascript"">window.location.replace(""" & strURL & """)</script> -->")
Response.Write("</head><body></body></html>")
' send the response
Response.End()
End If
End If
End If
' manage page URL redirects - that reach here because they bypass the built-in navigation
' ie Spiders, saved favorites, hand-crafted urls etc
If _portalSettings.ActiveTab.Url <> "" And Request.QueryString("ctl") Is Nothing Then
'check if the requested file exists
If Not System.IO.File.Exists(Server.MapPath(Request.Url.LocalPath)) Then
'Target Url
Dim redirectUrl As String = _portalSettings.ActiveTab.FullUrl
If _portalSettings.ActiveTab.PermanentRedirect Then
'Permanently Redirect
Response.StatusCode = 301
Response.AppendHeader("Location", redirectUrl)
Else
'Normal Redirect
Response.Redirect(redirectUrl, True)
End If
End If
End If
I just invert the .aspx and url redirect blocks, with a supplementary check of the file on the server in the redirect url block.