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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.