我试图在开发站点的子域上设置本质上的配置文件。"配置文件"将由用户创建,即可以在任何时候发生。
在本地,我知道我不能使用通配符主机条目或任何东西(在MAMP环境中运行,OS 10.6),而且我不确定如何动态创建条目。该站点运行在虚拟主机上。
下一个障碍当然是生产服务器,这是一个Media Temple gs(共享)服务器。同样,不知道如何自动创建这些(在生产服务器的情况下)DNS条目。
任何帮助/建议都是非常感谢的!您要查找的内容可能如下:
DNS:在您的DNS (*.example.com)中创建一个"catch-all" 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']变量中的用户名
我不确定我理解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命令,工作原理与此完全相同。 -
ISWIN
-定义如下:define('ISWIN', strpos(strtolower(php_uname()),'win')!==false && strpos(strtolower(php_uname()),'darwin')===false );