subdomains on localhost vs. production server under CodeIgniter
I'm trying to set up what are essentially profiles on subdomains on a development site. The 'profiles' would be user-created, i.e. can happen at any time.
Locally, I know I can't use wildcard host entries or anything (running in a MAMP environment, OS 10.6), and I'm not sure how to go about dynamically creating entries. The site runs on a vhost.
The next hurdle of course is the production server, which is a Media Temple gs (shared) server. Once again, no idea how to go about creati开发者_StackOverflow社区ng these (in the case of the production server) DNS entries automatically.
Any assistance/advice is very much appreciated!
What you're looking for might be the following:
DNS:
Create a "catch-all" A-entry in your DNS (*.example.com).
Apache configuration:
Add the following to your .htaccess file:
RewriteEngine On
# Extract the username from the subdomain
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^$ /profile.php?username=%1 [L]
PHP:
In your profile.php you suddenly have the username in the $_GET['username'] variable.
I'm not sure I understand what CI has to do with virtual hosts, but as of late, I created my own VHost functions:
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;
}
Since I basically copy+pasted it, you might want to replace some stuff:
ServerCentral::path()
- Absolute path to the current running script.System::execute
- Runs a shell command, works exactly like this.ISWIN
- Defined like this:define('ISWIN', strpos(strtolower(php_uname()),'win')!==false && strpos(strtolower(php_uname()),'darwin')===false );
精彩评论