我正在尝试在开发网站上设置子域上的基本配置文件。 “个人资料”将由用户创建,即可以随时发生。
在本地,我知道我不能使用通配符主机条目或任何东西(在MAMP环境中运行,OS 10.6),我不知道如何动态创建条目。该网站在虚拟主机上运行。
当然,下一个障碍是生产服务器,它是Media Temple gs(共享)服务器。再一次,不知道如何自动创建这些(在生产服务器的情况下)DNS条目。
非常感谢任何帮助/建议!
答案 0 :(得分:2)
您正在寻找的内容可能如下:
<强> DNS:强>
在您的DNS(* .example.com)中创建“全能”A条目。
Apache配置:
将以下内容添加到.htaccess文件中:
RewriteEngine On
# Extract the username from the subdomain
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^$ /profile.php?username=%1 [L]
<强> PHP:强>
在你的profile.php中,你突然在$ _GET ['username']变量中拥有用户名。
答案 1 :(得分:0)
我不确定我是否理解CI与虚拟主机有什么关系,但是最近我创建了自己的VHost功能:
define('VHOST_FILE','/etc/httpd/conf.d/vhost.conf');
/**
* Generate Apache vhost entry.
* @param string $domain Domain or subdomain name (eg: foo.example.com or example.biz).
* @param string $path Path to document root.
* @return string The generated vhost entry.
*/
function vhost_gen($domain,$path){
$eol=ISWIN ? CRLF : LF;
return '<VirtualHost *:80>'.$eol.
TAB.'ServerName '.str_replace(array(' ',CR,LF,TAB,NULL),'',$domain).$eol.
TAB.'DocumentRoot "'.str_replace(array(CR,LF,TAB,NULL),'',$path).'"'.$eol.
'</VirtualHost>'.$eol.$eol;
}
/**
* Writes the new vhost config file.
* @param array $items List of objects(domain,docroot) used in config.
*/
function vhost_write($items){
$eol=ISWIN ? CRLF : LF;
$data='NameVirtualHost *:80'.$eol.$eol;
foreach($items as $item)
$data.=vhost_gen ($item->domain,$item->docroot);
return @file_put_contents(VHOST_FILE,$data);
}
/**
* Returns a list of vhost entries.
* @return array List of objects(id,domain,docroot) entries.
*/
function vhost_read(){
$entries=array();
$lines=explode(LF,str_replace(CR,LF,@file_get_contents(VHOST_FILE)));
$entry=null;
foreach($lines as $line){
// parse line
$line=explode(' ',str_replace(TAB,' ',trim($line)),2);
if(isset($line[0]))$line[0]=strtolower(trim($line[0]));
// state engine
if($line[0]=='<virtualhost'){
$entry=new stdClass();
continue;
}
if($line[0]=='</virtualhost>' && $entry!==null){
$entry->id=count($entries)+1;
$entries[$entry->id]=$entry;
$entry=null;
continue;
}
if($line[0]=='servername' && $entry!==null){
$entry->domain=str_replace('"','',trim($line[1]));
continue;
}
if($line[0]=='documentroot' && $entry!==null){
$entry->docroot=str_replace('"','',trim($line[1]));
continue;
}
}
return $entries;
}
/**
* Backup vhost config file.
* @return boolean True on success, false otherwise.
*/
function vhost_backup(){
if(!@file_exists(VHOST_FILE))
return @file_put_contents(VHOST_FILE,'')
&& @copy(VHOST_FILE,ServerCentral::path().'vhost.conf.bak');
return @copy(VHOST_FILE,ServerCentral::path().'vhost.conf.bak');
}
/**
* Restore vhost backup file.
* @return boolean True on success, false otherwise.
*/
function vhost_restore(){
return @copy(ServerCentral::path().'vhost.conf.bak',VHOST_FILE);
}
/**
* Restarts apache/httpd service daemon.
* @return boolean True on success, false otherwise.
*/
function httpd_restart(){
$r=System::execute('service httpd restart');
return $r['return']==0;
}
由于我基本上复制+粘贴它,你可能想要替换一些东西:
ServerCentral::path()
- 当前正在运行的脚本的绝对路径。System::execute
- 运行shell命令,其工作方式与this完全相同。ISWIN
- 定义如下:define('ISWIN', strpos(strtolower(php_uname()),'win')!==false && strpos(strtolower(php_uname()),'darwin')===false );