我创建了具有自动创建子域功能的CMS
示例:用户按下按钮创建域,我在数据库中添加了子域名 john 并创建了文件夹 domain.com/subdomains/john.domain.com 现在我需要将此子域 john.domain.com 重定向到目录 domain.com/subdomains/john.domain.com
代码web.php:
if (!function_exists('getSubDomain')) {
function getSubDomain($url)
{
$domain = $url = str_replace(Request::getRequestUri(),'', $url);
$url = str_replace('http:','',$url);
$url = str_replace('//','',$url);
$url = str_replace('www.','',$url);
$url = str_replace('domain.com','',$url);
$url = rtrim($url,'.');
$url = ltrim($url,'.');
if($url){
$user = ['john','barry','wells','cisco'];
if(isset(array_flip($user)[$url])){
return true;
}
}
return false;
}
}
Route::group(['domain' => '{sub}.domain.com'], function(){
if(getSubDomain(Request::url())){
//Here need redirect to folder.
echo "Subdomain";
exit;
}
});