Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create subdomain programmatically in PHP

Tags:

php

I am on shared hosting and on a add on domain.

I need to create subdomain for each user of my website like if the username is jeff then he should have a url jeff.mydomain.com.

How can I create it programmatically using PHP?

like image 613
Prady Avatar asked Sep 18 '25 06:09

Prady


2 Answers

There's two parts to this. Firstly you'll need to setup a wildcard dns entry.

Once you've got that setup you should have all your requests pointed back to a single domain. From there you can then use php to figure out which domain you're currently on:

$domain = $_SERVER['HTTP_HOST'];
$base = 'mydomain.com';
$user = substr($domain, 0, -(strlen($base)+1));// the user part of the domain
if(!empty($user)) {
  $user = sanatiseUser($user);
  require_once $user.'.php';
}
like image 175
Ben Rowe Avatar answered Sep 19 '25 21:09

Ben Rowe


You need to set apache to listen for all domains coming into a specific IP.

You then need to setup a wildcard DNS entry to point *.domain.com to that IP.

Then inside your app, use $_SERVER['HTTP_HOST'] to determine which user to load.

like image 35
Mitch Dempsey Avatar answered Sep 19 '25 19:09

Mitch Dempsey