0

I'm currently creating a custom application which involves letting each users have its own subdomain but I'm finding it hard to make that work.

I want user to do this on the fly during registration.

How do I make this work using PHP, .htaccess and MySQL?

This is what I've done so far.

<?php
$host = $_SERVER ['HTTP_HOST'] ; 

$subdomain = "myshop" ; 

$domain = "$subdomain.$host" ; 

echo $domain ;

?>
user2314737
  • 27,088
  • 20
  • 102
  • 114
Adetona
  • 27
  • 1
  • 6
  • This should be a good place to start. http://stackoverflow.com/questions/586129/create-subdomains-on-the-fly-with-htaccess-php – Alex Dec 18 '16 at 07:00

1 Answers1

0

First point all sub domains to your server with a wildcard DNS-entry

*.example.com

when a user navigates to user1.example.com, have your php script look up the sub domain

$user = array_shift((explode(".",$_SERVER['HTTP_HOST'])));

check if the user exists in the database, or show an error.

You now have the username available and can use that parameter for querying the database.

When a user registers on your site, just create the user in the database and then redirect them to newuser.example.com

Johan Blomgren
  • 608
  • 4
  • 10
  • How do I create the url ? e.g newuser.example.com – Adetona Dec 18 '16 at 12:29
  • you don't need to "create" it. if you set up your DNS to work with for example www.example.com, you then add a CNAME record in your DNS that points wildcard subdomain to www " CNAME * --> www ", then "all" subdomains will "work" so users can go to tttyyzz.example.com, and you have to check if tttyyzz is a user in the database – Johan Blomgren Dec 18 '16 at 13:21
  • where do you host your page? – Johan Blomgren Dec 18 '16 at 13:29
  • You're going to have to add all individual subdomains into your hosts file if you expect to have `sub.localhost` etc. (or whatever other domain) resolve properly to 127.0.0.1, no can do wildcards there. Otherwise, what @JohanBlomgren said, do it at your domain provider, or find a dynamic DNS provider if you don't have a fixed IP address to use as a name server. – Markus AO Dec 24 '16 at 20:18