如果我是对的,您可以在其中使用通配符ServerAlias
并使用 mod_rewrite 您可以将其重写为正确的文件,这样您的虚拟主机就会像
<VirtualHost *:80>
ServerName mysaasapp.com
ServerAlias *.mysaasapp.com
DocumentRoot /var/www/user
</VirtualHost>
你的 CNAME 就像
*.mysaasapp.com IN CNAME mysaasapp.com
我希望这有帮助
编辑:
将虚拟主机更改为
<VirtualHost *:80>
ServerName mysaasapp.com
ServerAlias *.mysaasapp.com
DocumentRoot [STANDARDFOLDER]
</VirtualHost>
并替换[STANDARDFOLDER]
为DocumentRoot
mysaasapp.com 的
并在您index.php
的顶部:
// 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
}
这是一个非常肮脏的解决方案,我希望它现在对你有用
更新:
起初我不知道你也想从自定义域重定向,
然后他们必须将 CNAME 添加到他们的 DNS 服务器/管理器
theirdomain.com IN CNAME thierusername.mysaasapp.com
如果您的 DNS 记录是这样的,那么所有子域都将转到您的 apache,它是否应该使用我给您的代码和 Vhost
更新 2:
lanzz 刚刚指出了一些我忘记的东西(facepalm),我正在检查 HOST 标头这是没有意义的,因为它会有,customdomain.com
所以你需要向你的用户询问他们的域(如果你没有共享 IP,让他们用 CNAME 指向,如果你没有共享 IP,您可以让他们使用 A 记录指向您的 IP)并从 HTTP_HOST 查找域,然后在您的数据库中搜索它应该可以工作!
一点点编辑的代码:
//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;
}