private string GetRedirectUrl(Uri baseUri, Uri baseUriWithPath, string url) { string portalAlias; PortalAliasInfo objPortalAlias; RewriterAction action; string baseUrl = ""; Uri uri; if (url.Contains("http")) { // absolut url uri = new Uri(url); } else { // relative url if (url.Contains('/')) { uri = new Uri(baseUri, url); baseUrl = baseUri.ToString(); } else { // the browser add the full path when no path present in url uri = new Uri(baseUriWithPath, url); baseUrl = baseUriWithPath.ToString(); } } UrlRewriteModuleUtils.RewriteUrl(uri, out portalAlias, out objPortalAlias, out action); if (!action.DoRedirect || string.IsNullOrEmpty(action.RedirectUrl)) { return(""); } string RedirectUrl = action.RedirectUrl; if (RedirectUrl + "/" == baseUri.ToString()) { RedirectUrl = "/"; } else if (RedirectUrl.Length > baseUrl.Length) { RedirectUrl = RedirectUrl.Remove(0, baseUrl.Length); } string Anchor = ""; if (url.Contains('#')) { Anchor = url.Substring(url.IndexOf('#')); } return(RedirectUrl + Anchor); return(""); }
protected void lbTestUrl_Click(object sender, EventArgs e) { string portalAlias; PortalAliasInfo objPortalAlias; RewriterAction action; try { Uri url = new Uri(tbUrl.Text); UrlRewriteModuleUtils.RewriteUrl(url, out portalAlias, out objPortalAlias, out action); if (action.DoRedirect) { if (action.Status == 302) { lUrlResult.Text = "Redirect " + action.Status + " (" + action.Raison + ") : " + action.RedirectUrl; } else { lUrlResult.Text = "Redirect 301 (" + action.Raison + ") : " + action.RedirectUrl; } } else if (action.DoNotFound) { lUrlResult.Text = "NotFound 404 (" + action.Raison + ")"; } else if (action.DoReWrite) { lUrlResult.Text = "Rewrite to : " + action.RewriteUrl; } lUrlResult.Text = action.RedirectUrl; } catch (Exception ex) { lUrlResult.Text = "Error : " + ex.Message; } }
private void RewriteUrl(HttpApplication app, out string portalAlias) { PortalAliasInfo objPortalAlias; RewriterAction action; UrlRewriteModuleUtils.RewriteUrl(app, out portalAlias, out objPortalAlias, out action); // save OriginalUrl & PortalId for Logging if (app != null) { app.Context.Items.Add("UrlRewrite:OriginalUrl", app.Request.Url.AbsoluteUri); app.Context.Items.Add("UrlRewrite:PortalId", (objPortalAlias == null ? -1 : objPortalAlias.PortalID)); } if (action.DoNotFound) { //string strURL = "ErrorPage.aspx?status=404&error=CustomRule"; app.Response.Clear(); app.Response.StatusCode = 404; app.Response.Status = "404 Not Found"; app.Response.AppendHeader("X-OpenUrlRewriter-404-Raison", action.Raison); int TabId404 = PortalController.GetPortalSettingAsInteger(UrlRewiterSettings.ErrorPage404Setting, objPortalAlias.PortalID, -1); if (TabId404 != -1) { //TabInfo errTab = tc.GetTab(errTabId, result.PortalId, true); string strURL = Globals.glbDefaultPage + "?TabId=" + TabId404.ToString(); var ps = new PortalSettings(TabId404, objPortalAlias); app.Context.Items.Add("PortalSettings", ps); if (app.Context.User == null) { app.Context.User = Thread.CurrentPrincipal; } app.Response.TrySkipIisCustomErrors = true; //spoof the basePage object so that the client dependency framework //is satisfied it's working with a page-based handler IHttpHandler spoofPage = new CDefault(); app.Context.Handler = spoofPage; app.Context.Server.Transfer("~/" + strURL, true); } else { const string errorPageHtmlHeader = @"<html><head><title>404 Page not found </title></head><body>"; const string errorPageHtmlFooter = @"</body></html>"; var errorPageHtml = new StringWriter(); errorPageHtml.Write("<br> 404 Fle not found "); errorPageHtml.Write("<br> Raison : " + action.Raison); errorPageHtml.Write("<div style='font-weight:bolder'>Administrators</div>"); errorPageHtml.Write("<div>Change this message by configuring a specific 404 Error Page.</div>"); errorPageHtml.Write(string.Format("<a href=\"//{0}\">Goto website</a>", objPortalAlias.HTTPAlias)); app.Response.Write(errorPageHtmlHeader); app.Response.Write(errorPageHtml.ToString()); app.Response.Write(errorPageHtmlFooter); } app.Response.End(); } else if (action.DoRedirect) { app.Context.Items.Add("UrlRewrite:RedirectUrl", action.RedirectUrl); app.Response.AppendHeader("X-Redirect-Reason", action.Raison); if (action.Status == 302) { app.Response.Redirect(action.RedirectUrl, true); } else { app.Response.Status = "301 Moved Permanently"; app.Response.AddHeader("Location", action.RedirectUrl); app.Response.End(); } } else if (action.DoReWrite) { RewriterUtils.RewriteUrl(app.Context, action.RewriteUrl); } }