if Im right you can use wild card in ServerAlias
and with mod_rewrite you can rewrite it to the right file so your vhosts gets like
<VirtualHost *:80>
ServerName mysaasapp.com
ServerAlias *.mysaasapp.com
DocumentRoot /var/www/user
</VirtualHost>
and your CNAME gets like
*.mysaasapp.com IN CNAME mysaasapp.com
I hope this helps
EDIT:
change the vhosts to
<VirtualHost *:80>
ServerName mysaasapp.com
ServerAlias *.mysaasapp.com
DocumentRoot [STANDARDFOLDER]
</VirtualHost>
and replace [STANDARDFOLDER]
with the DocumentRoot
of mysaasapp.com
and in your index.php
place on the top this:
// Array with all the subdomains who doesn't belong to users
$_notUsers = array("www");
// No code without a Boom,
$_domainParts = explode('.',$_SERVER[HTTP_HOST],-2);
// Check if subdomain is legit
if(!in_array($_domainParts[0],$_notUsers) && !empty($_domainParts)){
// get the real subdomain structure
$_sub = str_replace('.mysaasapp.com','',$_SERVER['HTTP_HOST']);
// Don't look at this it's horrible :'( but this makes it think that there is
// something in user
$_GET['user'] = $_sub;
// I dont know where you want it in the question its index.php and in comment its
// user.php so change this as you want it
include 'user.php';
// Exit so we dont have an user page and homepage together but only when
// using user.php comment this out when using index.php
exit;
// end
}
this is a really dirty solution to your problem
I hope it works for you now
UPDATE:
at first I didn't know you also want to redirect from a custom domain,
then they have to add a CNAME to their DNS server/Manager with
theirdomain.com IN CNAME thierusername.mysaasapp.com
and if your DNS records are so that all the subdomain are going to your apache should it be working with the code and the Vhosts I gave you
UPDATE 2:
lanzz just pointed out something I forgot (facepalm) im checking the HOST header what makes no sense because it will have customdomain.com
so you need to ask your users for their domains (if you dont have a shared IP let them point with a CNAME and if you dont have a shared IP you can let them point to your IP with an A record) and lookup the domain from HTTP_HOST and search for that in your database after that it should work!
little bit of edited code:
//first look if you're on your own domain,
// also again an Array with all the subdomains who doesn't belong to users
$_notUsers = array("www");
//lets explode the domain
$_domainParts = explode(".",$_SERVER["HTTP_HOST"]);
//get length of $_domainParts
$_domainPartsLength = count($_domainParts);
//get last two parts (little bit ugly but there are uglier ways)
$_currentDomain = $_domainParts[$_domainPartsLength-1] . "." . $_domainParts[$_domainPartsLength-2];
//time to check if it's yours and if it's legit
if($_currentDomain == "mysaasapp.com"){
if(!in_array($_domainParts[0],$_notUsers) && !empty($_domainParts)){
// get the real subdomain structure
$_sub = str_replace('.mysaasapp.com','',$_SERVER['HTTP_HOST']);
// Don't look at this it's horrible :'( but this makes it think that there is
// something in user
$_GET['user'] = $_sub;
// I dont know where you want it in the question its index.php and in comment
// its user.php so change this as you want it
include 'user.php';
// Exit so we dont have an user page and homepage together but only when
// using user.php comment this out when using index.php
exit;
}
// here is your standard index (sorry for the odd placing)
}else{
//now here are we gonna play with the custom domain
// this function should return the username if the hostname is found in the
// database other wise it should return false
$_user = userGetByDomain($_SERVER["HTTP_HOST"]);
if($_user === false){
// tell the browser its not legit
header("Status: 404 Not Found");
//show your 404 page
include '404.php';
// and die
exit;
}
// now we know everything is okay we are gonna do the same thing as above
$_GET['user'] = $_user;
// I dont know where you want it in the question its index.php and in comment
// its user.php so change this as you want it
include 'user.php';
// Exit so we dont have an user page and homepage together but only when
// using user.php comment this out when using index.php
exit;
}
*.mysaasapp.com
– Bedcover