Skip to main content

Create Subdomains on the fly with .htaccess (PHP)


I am looking to create a system which on signup will create a subdomain on my website for the users account area.



e.g. johndoe.website.com



I think it would be something to do with the .htaccess file and possibly redirecting to another location on the website? I don't actually know. But any information to start me off would be greatly appreciated.



Creating a sign up area is not the problem - i have done this many a time. i am just unsure where to start with the subdomain.



Thanks, Ben.


Source: Tips4allCCNA FINAL EXAM

Comments

  1. In addition to setting up a DNS wildcard, you might want to take a look at Dynamic Mass Virtual Hosting for Apache which is how I've solved this in the past

    ReplyDelete
  2. It's nothing to do with .htaccess. You'll need to set up DNS records and virtual hosting for the subdomains.

    ReplyDelete
  3. You could allow every subdomain in the first place and then check if the subdomain is valid. For example:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com$
    RewriteRule !^index\.php$ index.php [L]


    Inside the index.php you can than extract the subdomain using:

    if (preg_match('/^([^.]+)\.example\.com$/', $_SERVER['HTTP_HOST'], $match)) {
    var_dump($match[1]);
    }


    But all this requires that your webserver accepts every subdomain name.

    ReplyDelete
  4. Mod_vhost_alias is the right module to do this.

    With one line you can tell Apache to look at the right place, with directory hashing, etc.
    For example, the line:

    VirtualDocumentRoot /http/users/%3.1/%3.2/%3


    would tell Apache to set the document root to /http/users/s/u/subdomain when requested for subdomain.yourdomain.com

    ReplyDelete
  5. I think the wild card DNS with Apache's Dynamic Mass Virtual Hosting is a reasonable solution also. Although, I have never tried it.

    If you have the need to scale out to multiple servers or the other solutions just don't work for you, I recommend using a database driven DNS server. I have successfully used MyDNS in the past. Since it uses MySQL (or PostgreSQL) you can update your DNS on the fly with PHP or just about anything else. The code doesn't look like it has been updated in a while, but it's DNS and therefore not exactly cutting edge.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex