Using HTTPModule to rewrite subdomains?

Discussion in 'C#' started by ScepterMT, Feb 21, 2008.

  1. #1
    Can anyone explain to me how to use the HTTPModule to rewrite my urls to support a sort of dynamic subdomains.

    I'm trying to convert:
    http://sceptermt.sceptermarketingtechnologies.com/

    into:
    http://www.sceptermarketingtechnologies.com/default.aspx?user=sceptermt.

    Is there a way to do this without having to use a host that supports the (*) wildcard character for dns?

    Any pointers would be very helpful.
     
    ScepterMT, Feb 21, 2008 IP
  2. mrbrantley

    mrbrantley Member

    Messages:
    87
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    mrbrantley, Feb 22, 2008 IP
  3. ScepterMT

    ScepterMT Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    OK, so I've changed my mind about the sub-domain part of it due to fighting with dns. Instead I'm using my HttpModule rewrite the virtual path and load the info extracted from the url into cookies. Here's a rough draft of the HttpModule that I wrote...
    public class HttpRewrite : IHttpModule
    {
    	public HttpRewrite()
    	{
    	}
    
        public string ModuleName
        {
            get
            {
                return "HttpRewrite";
            }
        }
    
        public void Dispose()
        {
            application.Dispose();
        }
    
        // Make the current application request and context available.
        private HttpApplication application;
        private HttpContext context;
    
        public void Init(HttpApplication app)
        {
            application = app;
            context = app.Context;
            app.BeginRequest += new EventHandler(this.app_BeginRequest);
        }
    
        private void app_BeginRequest(Object source, EventArgs e)
        {
            // Get the file path of the current request
            string strPath = context.Request.FilePath;
            //remove the leading '/' from the file path
            strPath = strPath.Substring(1);
    
            // make sure the file path is not empty
            if (strPath != "")
            {
                // make sure path does not include the Money_Makers directory
                if (!(strPath.IndexOf("Money_Makers", StringComparison.InvariantCultureIgnoreCase) >= 0))
                {
                    // create the delimiter '/' for splitting file path into an array
                    char[] delimiter = { '/' };
    
                    // split the file path into a string array
                    string[] strPathArray = strPath.Split(delimiter);
    
                    // find out how many parameters where sent in the file path
                    switch (strPathArray.GetUpperBound(0))
                    {
                        // if '0' there are no params and only the file name exists
                        case 0:
                            {
                                LoadDefaults();
                                break;
                            }
                        // if '1' there is a referrer and file name
                        case 1:
                            {
                                context.Server.Transfer(LoadAndCreateURL(strPathArray[0], strPathArray[1]));
                                break;
                            }
                        // if '2' there is a referrer, ad-tracker, and a file name
                        case 2:
                            {
                                context.Server.Transfer(LoadAndCreateURL(strPathArray[0], strPathArray[1], strPathArray[2]));
                                break;
                            }
                        // if default there are too many params in file path
                        default:
                            {
                                LoadDefaults();
                                context.Server.Transfer("~/Default.aspx");
                                break;
                            }
                    }
                }
            }
            else
            {
                LoadDefaults();
            }
        }
    
        private void LoadDefaults()
        {
            // Make sure Referrer has not been set
            if (context.Request.Cookies["Referrer"] == null)
            {
                // Load the Default User as the Referrer
                context.Response.Cookies["Referrer"].Value = ConfigurationSettings.AppSettings["DefaultUser"];
                context.Response.Cookies["Referrer"].Expires = DateTime.Now.AddDays(180);
            }
    
            // Make sure AdTracker has not been set for this session
            if (context.Request.Cookies["AdTracker"] == null)
            {
                // Load the AdTracker with a Default value
                context.Response.Cookies["AdTracker"].Value = "Default";
            }
        }
    
        private string LoadAndCreateURL(string strReferrer, string strFileName)
        {
            // Make sure the user in strReferrer exists
            if (Membership.GetUser(strReferrer) != null)
            {
                // Load the specified user as the referrer
                context.Response.Cookies["Referrer"].Value = strReferrer;
                context.Response.Cookies["Referrer"].Expires = DateTime.Now.AddDays(180);
            }
            // If the user in strReferrer does not exist
            else
            {
                // Make sure Referrer has not been set
                if (context.Request.Cookies["Referrer"] == null)
                {
                    // Load the default user as the referrer
                    context.Response.Cookies["Referrer"].Value = ConfigurationSettings.AppSettings["DefaultUser"];
                    context.Response.Cookies["Referrer"].Expires = DateTime.Now.AddDays(180);
                }
            }
    
            // Make sure AdTracker has not been set for this session
            if (context.Request.Cookies["AdTracker"] == null)
            {
                // Load the AdTracker with a Default value
                context.Response.Cookies["AdTracker"].Value = "Default";
            }
    
            return "~/" + strFileName;
        }
    
        private string LoadAndCreateURL(string strReferrer, string strAdTracker, string strFileName)
        {
            // Make sure the user in strReferrer exists
            if (Membership.GetUser(strReferrer) != null)
            {
                // Load the specified user as the referrer
                context.Response.Cookies["Referrer"].Value = strReferrer;
                context.Response.Cookies["Referrer"].Expires = DateTime.Now.AddDays(180);
            }
            // If the user in strReferrer does not exist
            else
            {
                // Make sure Referrer has not been set
                if (context.Request.Cookies["Referrer"] == null)
                {
                    // Load the default user as the referrer
                    context.Response.Cookies["Referrer"].Value = ConfigurationSettings.AppSettings["DefaultUser"];
                    context.Response.Cookies["Referrer"].Expires = DateTime.Now.AddDays(180);
                }
            }
    
            // Make sure AdTracker has not been set for this session
            if (context.Request.Cookies["AdTracker"] == null)
            {
                // Load the AdTracker with the value in strAdTracker
                context.Request.Cookies["AdTracker"].Value = strAdTracker;
            }
    
            return "~/" + strFileName;
        }
    }
    Code (markup):
    The code above works great. The problem I'm having is getting it to run for every request. It's weird, It seems to be running somewhat randomly, or maybe at the beginning of each session. It usually runs on the first request after compiling and then after that I'm getting 404 file not found errors. Here's how I've got it registered in my web.config...
    <httpModules>
          <add name="HttpRewrite" type="HttpRewrite"/>
    </httpModules>
    Code (markup):
    I'm running this on the asp.net development server, but I've also tried it under iis6 with the same results. Thanks for any suggestions!
     
    ScepterMT, Feb 23, 2008 IP
  4. Free Born John

    Free Born John Guest

    Messages:
    111
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you don't actually need an httprewrite module, you can include the code in the global.asax. That might help narrow down where the problem is. Also, don't forget it will be called for every request - including every image on the page.
     
    Free Born John, Feb 23, 2008 IP
  5. Forrest

    Forrest Peon

    Messages:
    500
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Forrest, Feb 25, 2008 IP