Skip to content

Instantly share code, notes, and snippets.

@squallstar
Created April 26, 2012 07:54
Show Gist options
  • Save squallstar/2497306 to your computer and use it in GitHub Desktop.
Save squallstar/2497306 to your computer and use it in GitHub Desktop.
Multiple subdomains on a single Bancha installation

Bancha multidomain plugin

How to install:

(soon)

<?php
class Bancha_Config extends CI_Config
{
// Replace the site_url() function with the function below
function site_url($uri = '', $prepend_language = TRUE)
{
$base = $this->slash_item('base_url');
if (is_string($prepend_language))
{
$base.= $prepend_language . '/';
} else {
if ($prepend_language && $this->prepend_language)
{
$base.= $this->prepend_language . '/';
}
}
if ($uri == '')
{
return $base.$this->item('index_page');
}
if ($this->item('enable_query_strings') == FALSE)
{
$suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
return domain_prepare( $base.$this->slash_item('index_page').$this->_uri_string($uri).$suffix );
}
else
{
return $base.$this->item('index_page').'?'.$this->_uri_string($uri);
}
}
}
<?php
class Bancha_Config extends CI_Config
{
// Replace the site_url() function with the function below
function site_url($uri = '', $prepend_language = TRUE)
{
$base = $this->slash_item('base_url');
if (is_string($prepend_language))
{
$base.= $prepend_language . '/';
} else {
if ($prepend_language && $this->prepend_language)
{
$base.= $this->prepend_language . '/';
}
}
if ($uri == '')
{
return $base.$this->item('index_page');
}
if ($this->item('enable_query_strings') == FALSE)
{
$suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
return domain_prepare( $base.$this->slash_item('index_page').$this->_uri_string($uri).$suffix );
}
else
{
return $base.$this->item('index_page').'?'.$this->_uri_string($uri);
}
}
}
<?php
// 1. -----------------------------------------------------------
define('ENVIRONMENT', 'development');
//Include the file just below the environment declaration
require_once('multidomain.php');
// 2. -----------------------------------------------------------
/*
*---------------------------------------------------------------
* ADMIN PUBLIC PATH
*---------------------------------------------------------------
*
*/
$admin_path = 'admin/';
//If is the admin, we redirect the user the the original domain
if (DEDICATED_SUBDOMAIN && $_SERVER['SERVER_NAME'] . '/' != DOMAIN_MAIN && strpos($_SERVER['REQUEST_URI'], '/admin') !== FALSE) {
header('Location: ' . DOMAIN_MAIN . $admin_path);
exit;
}
<?php
//The hostname
$hostname = 'mywebsite.com';
//The main subdomain (you can leave it blank)
$main_domain = '';
//The subdomains
$_SERVER['STANDALONE_DOMAINS'] = array(
'blog', 'about', 'works', 'shop'
);
/* --------------------------------------------------------------- */
// 1. Constants declaration
define('DOMAIN_MAIN', 'http://' . $main_domain . $hostname . '/');
define('DOMAIN_HOSTNAME', $hostname . '/');
// 2. URL Helper (called on the core/core/Core_Config.php)
function domain_prepare($url = '') {
// 2.1 trim the domain/subdomain
foreach ($_SERVER['STANDALONE_DOMAINS'] as $third_level) {
if (strpos($url, DOMAIN_HOSTNAME . $third_level) !== FALSE) {
// 2.1.1 something/blog > blog.something
return str_replace('http://' . DOMAIN_HOSTNAME . $third_level, 'http://' . $third_level . '.' . rtrim(DOMAIN_HOSTNAME, '/'), $url);
}
}
// 2.2 normalize url to the main domain
return str_replace('http://' . $_SERVER['SERVER_NAME'] . '/', DOMAIN_MAIN, $url);
}
// 3. Fake requests
$dedicated_subdomain = FALSE;
foreach ($_SERVER['STANDALONE_DOMAINS'] as $third_level) {
if (strpos($_SERVER['HTTP_HOST'], $third_level.'.') !== FALSE) {
$_SERVER['REQUEST_URI'] = '/' . $third_level . $_SERVER['REQUEST_URI'];
$dedicated_subdomain = TRUE;
break;
}
}
define ('DEDICATED_SUBDOMAIN', $dedicated_subdomain);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment