How to do it?
Well, Hi All!
I'm developing a Free Web Hosting Site. And I'm stuck, so pleease help, if you can and if you want! :)
Every user will have the feature to add one or more subdomains to his account. For example, if the site is called: thehost.com, a user might want to create two subdomains: sub1 and sub2. So, he should be able to use the File Manager to manage his created subdomains and see the results at sub1.thehost.com and sub2.thehost.com. Now, in my File System I would like to structure the directories in the following way:
Where Superman is our username (He added the two subdomains noted above).
The problem: How to do this? I've read much about the htaccess files and I found several solutions:
Solution 1: To Write a perl program, which connects to the database, and selects path for a given virtualhost. The RewriteMap of the htaccess will execute the perl program and for the subdomain sub1, it will SELECT vpath FROM vhosts WHERE vname = "sub1". It will return: users/Superman/sub1/. The htaccess will substitute and everything works fine.
Now, I don't like this, because if everytime we access sub1.thehost.com, we make a select query, the mysql server (yes, i'm using mysql) will die in pain for sure.
Solution 2: Don't give the users feature to add subdomains, but when a user is created, we created a subdomain called superman.thehost.com. Now, this will work fine, because we only need the 'superman' subdomain name to find out where is our 'superman' subdomain path. This solution I would not like to use.
Please, If you know how to deal with this problem, tell me. I've pointed out two solutions, but if you know a third one....better one, please let me now.
Thank you in advance, Web Masters!
I've solved my problem, 10x for the replies...
Welcome to WebmasterWorld, ivanz.
Glad you got it sorted. As far as responses go, this happens often in forums ... especially over weekends and/or holidays, traffic slows a bit sometimes and so will responses. Then add the two together, weekend plus an extended holiday break like Christmas/New Year's and you see even less activity. Patience and planning, my friend, patience and planning.
If it were me, I guess I would set a new directory per user as you specified. Then, under each user I would have another folder called "subdomains" which would contain a folder for each subdomain created. This subdomain folder would contain any specifics for that subdomain, including a new document root for that particular subdomain.
Well, I haven't tested my solution, but here is how it works in simple words:
I have created a files directory and two subdirectories: users and sdsymlinks. Now, the 'users' subdirectory contains directories per users (Superman, Batman, user3, etc...). The new thing in the solution is the symbolic link in sdsymlinks...
If Superman register 'sub1', in 'users' folder we will create 'sub1' folder under the 'Superman' folder:
The next step is to create a symbolic link in sdsymlinks folder: sub1, which will point to ../users/Superman/sub1.
mod_rewrite will not lookup any information and i think it is beatufiul :) We just follow the symlink with the subdomain name and we are in the users/Superman/sub1 folder. Thus, the task is completed.
Notify me for any problems you might have with this solution.
And..sorry I got angry about the unanswered topic.
I think permissions would be easier to administer and maintain if the structure were more "self-contained" per user. For example, your user's directory would look something like ...
... and then, breaking down the user "Superman" and his directory structure ...
/users/Superman (user root)
/users/Superman/statistics (per-user logs here)
/users/Superman/subdomains/sub1 (a subdomain)
/users/Superman/subdomains/sub1/public_html (document_root for sub1 subdomain)
I'd prefer to keep it simple: Internally rewrite
superman.example.com --> /users/superman/ -and-
sub1.superman.com --> /users/superman/sub1/
No symlinks needed, no mySQL, just wild-card DNS and four RewriteRules: One rule to map subdomain requests to /users/subdomain/, another to map sub-subdomain requests to /users/subdomain/sub/, and two more rules to 301-redirect direct client requests for any subdomain and/or sub-subdomain directories back to the canonical subdomain format.
coopster: The structure I gave is the same as yours, with only difference that you have subdomains folder under the 'Superman' folder. The code bb tags in the forum are cutting the tabs and it doesn't look like a tree, but more like three separate folders.
I will keep the topic active, until I've finally tested the solution. Then I'll give the example code...
|The structure I gave is the same as yours |
Understood now, thanks.
I would not symlink either. No need to symlink. Each subdomain gets it's own VirtualHost container with corresponding filesystem path to subdomain as DocumentRoot.
Yes, but there is one thing missing in the <VirtualHost> solution: Batman enters (one of the users in the hosting site) and creates his subdomain. The apache needs a restart in order to find out the new <VirtualHost> directive, written in a file somewhere on the system. Is there another way to do it? Or when every user creates his subdomain, the apache restarts...mmm noot, as Borat said.
What are you going to do when a user first signs up? If restarting the Apache server is going to be an issue then you can put it on a scheduler and only run the restart command if new domains/subdomains have actually been created.
I think you would be surprised to know how often shared hosting providers restart their instances of Apache.
Ahaaa, I've searched for restaring apache and i found that one could restart it gracefully. This will leave any threads to finish their requests. The main thread will reread its configuration files and reopen its logs.
So, coopster, thank you for the pointers. I think the virtual host method is better, because the symbolic links are doable only on unix like systems and vista (mklink).
To restart apache gracefully, one should call httpd.exe (we're talking about win) in this manner:
httpd.exe -k restart.
I didn't realize MS incorporated symbolic linking into Vista (I don't use Vista personally so I would not know). On a Win XP Pro box or Windows Server there is a resource kit that has about 40 tools, one of them being linkd -- a symbolic linking command.