creating sub domains... or custom user directories

Posted on 2006-05-15
Last Modified: 2012-05-05
Hey all,

I have a system where users register and create a blog. I want the system to create a custom domain such as


I don't want a million directories however in the system... any thoughts as to how I could do this.

Question by:phenixfilms
    LVL 16

    Accepted Solution

    you can use mod_rewrite in apache in a way that redirects to

    also, you can create that manually, add error handler to 404(file not found) error via apache, say a file named redirect.php, this file can handle the redirection itself, e.g., it checks the desired url and correlate it with the appropriate one from the db and redirects the user to it, if no match is found, and error page saying page can't be found is published :)
    LVL 1

    Assisted Solution


    I did something simular on one of my sites. The following code gets the requested URL, example, and then gets the 'myname' part from the URL.

    Once you have the 'myname' part you can forward the user to which ever page you want.

    //get the url
    $host = $_SERVER['HTTP_HOST'];
    //check that the main site wasn't requested
    if(strtolower(substr($host, 0, 3))!=='www'){
          //get the sub domain
          $findDot = strpos($host, '.');
          //if found
                //lower case
                $subdomain = strtolower(substr($host, 0, $findDot));
                if($subdomain == "mydomain"){
                      header('Location: apage.html');

    It worked for me, so hopefully your hosting setup will work with it too.

    LVL 1

    Expert Comment


    I forgot to mention this code should go on your index page...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to count occurrences of each item in an array.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now