1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / CustomHttpModule RewritePath

CustomHttpModule RewritePath

9 posts, 0 answered
  1. Court
    Court avatar
    20 posts
    Registered:
    24 Nov 2009
    25 Feb 2010
    Link to this post
    Using the latest verison of Sitefinity, in a custom IHttpModule how would I go about executing this:

    HttpContext.Current.RewritePath(destination);

    Without Telerik's CmsHttpModule overriding the new RewritePath?

    Or could we write a custom TelerikCmsHttpModule?

    Here is our Code:
    private void Application_BeginRequest(Object source, EventArgs e)
            {
                HttpContext context = HttpContext.Current;
     
                string destination = GetUrlRewrite(context);
                if (!string.IsNullOrEmpty(destination))
                {
                    HttpContext.Current.RewritePath(destination);
                }
            }

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    25 Feb 2010
    Link to this post
    Hello Court,

    You could create a custom HttpModule that inherits from IHttpModule or custom HttpHandler and replace the default one in the web.config file. Most of the methods in CmsHttpModule are private and cannot be overriden and accessed. Sitefinity also has CmsHttpModuleUrlRewrite module that you can use for rewriting.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. Court
    Court avatar
    20 posts
    Registered:
    24 Nov 2009
    25 Feb 2010
    Link to this post

    Hi Ivan. Thanks for responding.

    I replaced the Telerik CmsHttpModule with a custom http module and when the httpModule inherited from IHttpModule the RewritePath did not work. I got an application error:

    "The Resource can not be found. HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable."
     Requested URL: /Landing-Pages/City-Landing-Page.aspx

    So then I modified the httpModule to inherit from the Telerik CmsHttpModule and that didn't work either. It just took me to the default home page instead of doing the RewritePath.

    Here is my code for the httpModule inheriting from IHttpModule:

    using System;
    using System.Net;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.IO;
    using System.Web;
    using System.Web.Mail;
    using System.Security;
    using System.Security.Cryptography;
    using System.Text;
      
    using Foundation.Helpers;
    using Envision.Data.LIA.Core;
      
    namespace Foundation.HttpModules
    {
      
        public class LIAHttpModule : IHttpModule 
        {
            public LIAHttpModule()
            {
            }
      
            public void Init(HttpApplication application)
            {
                application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
            }
      
            private void Application_BeginRequest(Object source, EventArgs e)
            {
                HttpContext context = HttpContext.Current;
      
                if (ExcludeFromRewrite(context))
                {
                    return;
                }
                else
                {
                    string destination = GetUrlRewrite(context);
                    if (!string.IsNullOrEmpty(destination))
                    {
                        HttpContext.Current.RewritePath(destination);
                    }
                }
            }
      
            private bool ExcludeFromRewrite(HttpContext Context)
            {
                string fullurl = Context.Request.RawUrl.ToString();
                string contextPath = Context.Request.Path;
                string page = contextPath.Substring(Context.Request.Path.LastIndexOf("/") + 1);
                string pageExt = Path.GetExtension(page);
                string[] extList = { ".ico", ".jpeg", ".jpg", ".gif", ".png", ".css", ".js", ".swf", ".ashx", ".axd" };
      
                //if (fullurl == "/")
                //{
                //    return true; ;
                //}
      
                if (extList.Contains(pageExt))
                {
                    return true;
                }
      
                if (SitefinityHelper.CheckEditMode(Context.Request))
                {
                    return true;
                }
      
                if (fullurl.ToLower().Contains("sitefinity"))
                {
                    return true;
                }
      
                if (fullurl.ToLower().Contains("city-landing-page"))
                {
                    return true;
                }
      
                //if (page.ToLower().Contains("default.aspx"))
                //{
                //    return true;
                //}
      
                if (fullurl.IndexOf("?404;") >= 0)
                {
                    //PageNotFound(fullurl);
                    return true;
                }
      
                return false;
            }
      
            private string GetUrlRewrite(HttpContext httpContext)
            {
                string destination = null;
                string domain = httpContext.Request.Url.Host;
                string page = null;
      
                using (CoreDataContext context = new CoreDataContext())
                {
                    CityMarket cm = context.CityMarkets.FirstOrDefault(a => a.DomainName == domain);
                    if (cm != null)
                    {
                        page = SitefinityHelper.GetPageUrlbyFileName(Configuration.GetSetting(AppSettings.CityLandingPage));
                        destination = page;
                    }
                }
      
                return destination;
            }
      
            private string UrlRewrite(HttpContext Context)
            {
                string[] extList = { ".aspx", ".asp", ".cfm", ".php", ".htm", ".html" };
                string fullurl = Context.Request.RawUrl.ToString();
                string page = Context.Request.Path.Substring(Context.Request.Path.LastIndexOf("/") + 1);
                string pageNoExt = Path.GetFileNameWithoutExtension(page);
                string pageExt = Path.GetExtension(page);
                string destination = string.Empty;
      
                if (page.ToLower() == "sitemap.xml")
                {
                    destination = "~/sitemap.xml.ashx";
                }
      
                if (extList.Contains(pageExt) && string.IsNullOrEmpty(destination))
                {
                    int offset = fullurl.IndexOf("?") + 1;
                    string querystring = fullurl.Substring(offset);
                    string urlpage = page.Substring(0, page.LastIndexOf("."));
                    destination = "~/Default.aspx?Page=" + urlpage;
      
                    //Check Sitefinity for page
                    string pageUrl = SitefinityHelper.GetPageUrlbyFileName(page);
                    if (!string.IsNullOrEmpty(pageUrl))
                    {
                        destination = pageUrl + "?Page=" + urlpage;
                    }
      
                    if (offset > 0)
                    {
                        destination += "&" + querystring;
                    }
                }
                return destination;
            }
      
            public void Dispose() { }
      
        }
    }


    Here is my code for the httpModule inheriting from Telerik's CmsHttpModule:

    using System;
    using System.Net;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.IO;
    using System.Web;
    using System.Web.Mail;
    using System.Security;
    using System.Security.Cryptography;
    using System.Text;
      
    using Telerik.Cms;
    using Telerik.Cms.Web;
    using Helpers = Foundation.Helpers;
    using Envision.Data.LIA.Core;
      
    namespace Foundation.HttpModules
    {
        public class LIACmsHttpModule : CmsHttpModule
        {
            public LIACmsHttpModule()
            {
                HttpApplication application = System.Web.HttpContext.Current.ApplicationInstance;
                application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
            }
      
            private void Application_BeginRequest(Object source, EventArgs e)
            {
                if (ExcludeFromRewrite(HttpContext.Current))
                {
                    return;
                }
                else
                {
                    string destination = UrlRewrite(HttpContext.Current);
      
                    if (!string.IsNullOrEmpty(destination))
                    {
                        HttpContext.Current.RewritePath(destination);
                    }
                }
            }
      
            private bool ExcludeFromRewrite(HttpContext Context)
            {
                string fullurl = Context.Request.RawUrl.ToString();
                string contextPath = Context.Request.Path;
                string page = contextPath.Substring(Context.Request.Path.LastIndexOf("/") + 1);
                string pageExt = Path.GetExtension(page);
                string[] extList = { ".ico", ".jpeg", ".jpg", ".gif", ".png", ".css", ".js", ".swf", ".ashx", ".axd" };
      
                //if (fullurl == "/")
                //{
                //    return true; ;
                //}
      
                if (extList.Contains(pageExt))
                {
                    return true;
                }
      
                if (Helpers.SitefinityHelper.CheckEditMode(Context.Request))
                {
                    return true;
                }
      
                if (fullurl.ToLower().Contains("sitefinity"))
                {
                    return true;
                }
      
                if (fullurl.ToLower().Contains("city-landing-page"))
                {
                    return true;
                }
      
                //if (page.ToLower().Contains("default.aspx"))
                //{
                //    return true;
                //}
      
                if (fullurl.IndexOf("?404;") >= 0)
                {
                    //PageNotFound(fullurl);
                    return true;
                }
      
                return false;
            }
      
            private string UrlRewrite(HttpContext Context)
            {
                string[] extList = { ".aspx", ".asp", ".cfm", ".php", ".htm", ".html" };
                string fullurl = Context.Request.RawUrl.ToString();
                string page = Context.Request.Path.Substring(Context.Request.Path.LastIndexOf("/") + 1);
                string pageNoExt = Path.GetFileNameWithoutExtension(page);
                string pageExt = Path.GetExtension(page);
                string destination = string.Empty;
      
                if (page.ToLower() == "sitemap.xml")
                {
                    destination = "~/sitemap.xml.ashx";
                }
      
                if (extList.Contains(pageExt) && string.IsNullOrEmpty(destination))
                {
                    int offset = fullurl.IndexOf("?") + 1;
                    string querystring = fullurl.Substring(offset);
                    string urlpage = page.Substring(0, page.LastIndexOf("."));
                    destination = "~/Default.aspx?Page=" + urlpage;
      
                    //Check Sitefinity for page
                    string pageUrl = Helpers.SitefinityHelper.GetPageUrlbyFileName(page);
                    if (!string.IsNullOrEmpty(pageUrl))
                    {
                        destination = pageUrl + "?Page=" + urlpage;
                    }
      
                    if (offset > 0)
                    {
                        destination += "&" + querystring;
                    }
                }
                return destination;
            }
      
            private string GetUrlRewrite(HttpContext httpContext)
            {
                string destination = null;
                string domain = httpContext.Request.Url.Host;
                string page = null;
      
                using (CoreDataContext context = new CoreDataContext())
                {
                    CityMarket cm = context.CityMarkets.FirstOrDefault(a => a.DomainName == domain);
                    if (cm != null)
                    {
                        page = Helpers.SitefinityHelper.GetPageUrlbyFileName(Configuration.GetSetting(AppSettings.CityLandingPage));
                        destination = page;
                    }
                }
      
                return destination;
            }
        }
    }


    So what we needing to accompllish is that this website has about 100 domains for each of the market cities that the client is doing a campaign on.  These special domains all need to go to a specific landing page in sitefinity.  The default URL and any other special domain that doesn't have content setup yet needs to go to the default home page.

    We have a table in the database that has the list of domains. So if the domain exisit in the database, then there is content for that domain and needs to go to the special landing page.  Otherwise the default home page should display.  

    All these domains will go to the same landing page. I have custom controls on that page that will read the domain and get the approiate content from siteinfinty using the siteinfity API.

    Any suggestions on the best way to do this?

  4. Gabe Sumner
    Gabe Sumner avatar
    440 posts
    Registered:
    09 Sep 2007
    25 Feb 2010
    Link to this post
    Hey Court,

    i'm trying to visualize what you want to accomplish.  Here is what I think you're describing:

    http://houston.mydomain.com -> http://www.mydomain.com/houston.aspx
    http://sofia.mydomain.com -> http://www.mydomain.com/sofia.aspx
    http://boston.mydomain.com -> http://www.mydomain.com/generic.aspx

    Boston redirects to "generic.aspx" because no special landing page exists for this city.  All other domains exist in your database and have a special landing page that you want to redirect users to.

    It seems like the best way to accomplish this would be to add a new HttpModule, which it seems like you're describing.  However, it sounds like you're replacing an existing Sitefinity Module, rather than merely adding your own alongside existing Sitefinity Modules.  

    A very simple example for adding a new HttpModule can be found here:


    Your code above is pretty involved so I can't claim a deep understanding of what it does or why it might be breaking.  However, my knee-jerk recommendation is to add an HttpModule and avoid replacing existing Sitefinity HttpHandlers or HttpModules.

    Gabe Sumner
    Telerik | Sitefinity CMS
  5. Court
    Court avatar
    20 posts
    Registered:
    24 Nov 2009
    25 Feb 2010
    Link to this post
    Hi Gabe, thanks so much for looking into this.

    Your are close to what we need..

    http://domain-houston.com --> cityLandingpage.aspx
    http://domain-boston.com --> citylandingpage.aspx
    http://domain-denver.com --> citylandpage.aspx

    http://domain-Seattle.com --> generic.aspx (only because no content has been created yet for this domain so this domain will not be in our database table yet)

    http://domain.com --> generic.aspx

  6. Court
    Court avatar
    20 posts
    Registered:
    24 Nov 2009
    26 Feb 2010
    Link to this post
    I did have a look at the article before and it doesn't quick do what we need...

    context.Response.Status="301 Moved Permanently";
     context
    .Response.AddHeader("Location", url);
    context
    .Response.End();


    I dont need to a 301 Move Permanently response. Just a RewritePath.

    The actual location of the landing page in siteinfity is:

    http://domain.com/landingpages/citylandingpage.aspx

    However, we don't want to expose that URL to browser client up in the URL address section nor do we want to expose that URL path to any search engine spiders.

    Is there any way to use the asp.net framework here?:
    HttpContext.Current.RewritePath(destination);
    Or if used the "context.Response.AddHeader("Location", url);" that will work just like RewritePath ?
  7. Court
    Court avatar
    20 posts
    Registered:
    24 Nov 2009
    26 Feb 2010
    Link to this post
    Gabe wrote: "It seems like the best way to accomplish this would be to add a new HttpModule, which it seems like you're describing.  However, it sounds like you're replacing an existing Sitefinity Module, rather than merely adding your own alongside existing Sitefinity Modules."

    We tried adding another httpModule along side with Sitefinity's CmsHttpModule and the RewritePath was getting overriden by SF CmsHttpModule. It was suggest by Ivan that we replace Sitefinity's CmsHttpModule with our own custom but that didn't work either as mentioned above.
  8. Court
    Court avatar
    20 posts
    Registered:
    24 Nov 2009
    03 Mar 2010
    Link to this post
    bump. This is still an issue for us for our client.  Need assistance please.
  9. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    05 Mar 2010
    Link to this post
    Hello Court,

    As far as I see it you can this solution -  Domain Page Mapping which does not require any custom modules or handlers. using domain page mapping you can have multiple domains and subdomains that resolve different or the same paths. You can manage all domains/pages from one Sitefinity instance.

    However if you want to use your module you can inherits from CmsHttpModule  and override GetUrl method. By default the method returns the context.Request.RawUrl, so you can return your "destination" here.

    using Telerik.Cms.Web;
     
    public class CustomCmsHttModule : CmsHttpModule
    {
       protected override string GetUrl(System.Web.HttpContext context)
       {
          string destination = GetUrlRewrite(context);
          if (!string.IsNullOrEmpty(destination))
           {
              return destination;
           }
         return base.GetUrl(context);
       }
    }


    Regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Register for webinar
9 posts, 0 answered