Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
3.50/5 (4 votes)
See more:
Hi,

I need an urgent idea/ how to create sub-domain for each user who is registering in my application..

For eg) I hosted my application in www.xyz.com
If a user with name "bsoft" is registered in my app.
I need to create a site for the user as "bsoft.xyz.com"
In simple words how to rewrite the url for each user??

Please help..
Posted
Comments
Sergey Alexandrovich Kryukov 31-Jan-13 2:41am    
Why? I guess, you have very few customers. But what if you need to scale it up?..
—SA
Joezer BH 31-Jan-13 2:49am    
Say Sergey, if we manage to get the people to ask the question instead of stating the solution they want, would the world be a better place (at least for their customers)?
Sergey Alexandrovich Kryukov 31-Jan-13 2:59am    
Maybe. At least, we would have a better looking, more useful CodeProject :-)

However, this question is actually not bad; in fact, this is still a real question, while too many inquirer's posts in this forum are not. And you may not see many of those non-questions, because I recently removed a lot of them... :-)

This is becoming a real disaster...

Thank you,
—SA
Srimathi89 31-Jan-13 3:25am    
When will I get a solution???
bbirajdar 31-Jan-13 4:52am    
I have seen somebody doing this is asp.net with C#. I dont have any code at the moment. Continue with your research and it can be done. Some references I can provide you

http://stackoverflow.com/questions/6638923/how-to-create-this-type-of-subdomain-on-the-fly-like-blogger-com

Hi
try with below code it may solve your problem

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Administration;
using Microsoft.Web.Administration;
using System.IO;


public partial class _Default : System.Web.UI.Page
{

    private const string SERVER_IP = "192.168.111.112";// put your ip address
  private const int PORT = 80;
  private const string WEB_DOMAIN_PATH = @"F:\\web\domains\{0}\";

    //Live server
    //private const string SERVER_IP = "192.168.111.111";

    protected void Page_Load(object sender, EventArgs e)
    {
   if (!string.IsNullOrEmpty(Request.QueryString["user"]))
   {

    try
    {
     string username = Request.QueryString["user"];
     string status = CreateUserSite(username, "codeproject.com");//change your Domain id

     Response.Write(status);
    }
    catch(Exception ex)
    {
     Response.Write(ex.Message); 
    }
   }
   else
   {
    Response.Write("user parameter not supplied"); 
   }

   
    }


    private string CreateUserSite(string user, string domain)
    {


   string path = string.Format(WEB_DOMAIN_PATH, domain);

        string userpath = path + user;

        string userUrl = user + "." + domain;

        using (ServerManager serverManager = new ServerManager())
        {

            bool siteExists = false;
            int number = serverManager.Sites.Where(p => p.Name.ToLower().Equals(userUrl.ToLower())).Count();

            if (number == 0)
            {
                siteExists = false;
            }
            else
            {
                siteExists = true;
            }

            if (!siteExists)
            {

                //create user directory
                Directory.CreateDirectory(userpath);

                //copy every files from a-base to a new created folder
                FileInfo[] d = new DirectoryInfo(path + @"\a-base").GetFiles();
                foreach (FileInfo fi in d)
                {
                    File.Copy(fi.FullName, userpath + @"\" + fi.Name, true);
                }

                //create a directory
                Directory.CreateDirectory(userpath + @"\swfobject");

                FileInfo[] d1 = new DirectoryInfo(path + @"\a-base\swfobject").GetFiles();
                foreach (FileInfo fi in d1)
                {
                    File.Copy(fi.FullName, userpath + @"\swfobject\" + fi.Name, true);
                }



                //create site
                Site mySite = serverManager.Sites.Add(userUrl, path + user, PORT);
                mySite.ServerAutoStart = true;
                mySite.Applications[0].ApplicationPoolName = domain;

                //create bindings
                mySite.Bindings.Clear();
                mySite.Bindings.Add(string.Format("{0}:{2}:{1}", SERVER_IP, userUrl, PORT ), "http");
                mySite.Bindings.Add(string.Format("{0}:{2}:www.{1}", SERVER_IP, userUrl, PORT), "http");


                Configuration config = serverManager.GetApplicationHostConfiguration();
                ConfigurationSection httpLoggingSection = config.GetSection("system.webServer/httpLogging", userUrl);
                httpLoggingSection["dontLog"] = true;

                serverManager.CommitChanges();

              //  ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "success", "alert('" + userUrl + " created');", true);

            }
            else
            {
                //ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "error", "alert('user exists. Please use other name');", true);
       throw new Exception("user exists. Please use other name");
      }


      return userUrl + " has been successfully created"; 
        }
    }
}
 
Share this answer
 
Comments
DrTJ2066 7-Aug-14 17:59pm    
Nice approach, but how can I do it in a sharing environment without the access to the configuration file?!
I receive this error:
Filename applicationHost.config Error Cannot write configuration file due to insufficient permissions
heresanjay01 23-Nov-15 0:16am    
C:\Windows\System32\inetsrv
give full permission to iis user
1-config folder
2-and 3 config file in config folder
Hi srimathi
According to me you have to contact with your Hosting Server Vendor may they will provide you API for this.
 
Share this answer
 
Comments
Srimathi89 31-Jan-13 4:09am    
Hi,
But I host it in my own server.. How could I do it????
Aarti Yadav 20-Mar-15 11:50am    
is that code working properly ,
Aarti Yadav 13-Apr-15 4:40am    
have u create url like subdomain for each login..
[no name] 31-Jan-13 4:43am    
use the above code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900