From dfb3f7420aef24dbbf58782999d09ad1d5ed0f52 Mon Sep 17 00:00:00 2001 From: Toni Haryanto Date: Sat, 3 Jan 2015 05:52:19 +0700 Subject: [PATCH] fix subfolder based multisite --- .gitignore | 3 +- system/application/config/pusaka.php | 2 +- system/application/core/MY_Config.php | 53 ++++++++++++++++++++++ system/application/core/MY_URI.php | 26 ++++++++++- system/application/hooks/MyHookClass.php | 9 ++-- system/application/libraries/MX/Config.php | 44 ------------------ 6 files changed, 87 insertions(+), 50 deletions(-) create mode 100644 system/application/core/MY_Config.php diff --git a/.gitignore b/.gitignore index 4727433..ba93cb3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -dploy.yaml \ No newline at end of file +dploy.yaml +sites/_domain/pusakadev.com.conf \ No newline at end of file diff --git a/system/application/config/pusaka.php b/system/application/config/pusaka.php index b9c75b8..5b721d2 100644 --- a/system/application/config/pusaka.php +++ b/system/application/config/pusaka.php @@ -16,7 +16,7 @@ | live domain |-------------------------------------------------------------------------- | -| Domain in live server to enable subsite mode. +| Domain in live server to enable subsite mode, example 'yoursite.com'. | If it is set, your site domain will become http://yoursite.com/siteslug/ | and so your multisite. | Default to false. diff --git a/system/application/core/MY_Config.php b/system/application/core/MY_Config.php new file mode 100644 index 0000000..a038689 --- /dev/null +++ b/system/application/core/MY_Config.php @@ -0,0 +1,53 @@ +slash_item('base_url').$this->item('index_page').$site_slug; + } + + $uri = $this->_uri_string($uri); + + if ($this->item('enable_query_strings') === FALSE) + { + $suffix = ($this->item('url_suffix') === FALSE) ? '' : $this->item('url_suffix'); + + if ($suffix !== '' && ($offset = strpos($uri, '?')) !== FALSE) + { + $uri = substr($uri, 0, $offset).$suffix.substr($uri, $offset); + } + else + { + $uri .= $suffix; + } + + return $this->slash_item('base_url').$this->slash_item('index_page').$site_slug.$uri; + } + elseif (strpos($uri, '?') === FALSE) + { + $uri = '?'.$uri; + } + + return $this->slash_item('base_url').$this->item('index_page').$site_slug.$uri; + } +} \ No newline at end of file diff --git a/system/application/core/MY_URI.php b/system/application/core/MY_URI.php index fb48118..f708c35 100644 --- a/system/application/core/MY_URI.php +++ b/system/application/core/MY_URI.php @@ -19,8 +19,13 @@ protected function _detect_uri() return ''; } + include APPPATH.'config/pusaka.php'; + + $domain = $_SERVER['HTTP_HOST']; + // if it's localhost, then make uri begin from segment 2 - if($_SERVER['HTTP_HOST'] == 'localhost'){ + if($domain == $config['localhost_domain']){ + if (strpos($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']) === 0) { $uri = substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME'].'/'.SITE_SLUG)); @@ -33,6 +38,25 @@ protected function _detect_uri() { $uri = $_SERVER['REQUEST_URI'].'/'.SITE_SLUG; } + + } + + // if subfolder base multisite chosen + elseif($domain == $config['subsite_domain']){ + + if (strpos($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']) === 0) + { + $uri = substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME'].'/'.SITE_SLUG)); + } + elseif (strpos($_SERVER['REQUEST_URI'], dirname($_SERVER['SCRIPT_NAME'])) === 0) + { + $uri = substr($_SERVER['REQUEST_URI'], strlen(dirname($_SERVER['SCRIPT_NAME']).'/'.SITE_SLUG)); + } + else + { + $uri = $_SERVER['REQUEST_URI'].'/'.SITE_SLUG; + } + } else { if (strpos($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']) === 0) { diff --git a/system/application/hooks/MyHookClass.php b/system/application/hooks/MyHookClass.php index d684040..226f940 100644 --- a/system/application/hooks/MyHookClass.php +++ b/system/application/hooks/MyHookClass.php @@ -4,13 +4,16 @@ function multisite() { - include_once APPPATH.'config/pusaka.php'; + include APPPATH.'config/pusaka.php'; $domain = $_SERVER['HTTP_HOST']; // if it is a local server - if($domain == $config['localhost_domain'] || $domain == $config['subsite_domain']) { - $uri = substr($_SERVER['REQUEST_URI'], strlen(dirname($_SERVER['SCRIPT_NAME'].'/'))); + if($domain == $config['localhost_domain'] || $domain == $config['subsite_domain']){ + $uri = ($domain == $config['localhost_domain']) + ? substr($_SERVER['REQUEST_URI'], strlen(dirname($_SERVER['SCRIPT_NAME'].'/'))) + : $uri = $_SERVER['REQUEST_URI']; + $segments = explode('/', $uri); if(isset($segments[1]) && !empty($segments[1])) { diff --git a/system/application/libraries/MX/Config.php b/system/application/libraries/MX/Config.php index 25128e5..c1eb6b3 100644 --- a/system/application/libraries/MX/Config.php +++ b/system/application/libraries/MX/Config.php @@ -68,48 +68,4 @@ public function load($file = 'config', $use_sections = FALSE, $fail_gracefully = return $this->item($file); } } - - /** - * Site URL - * Returns base_url . index_page [. uri_string] - * - * @param mixed the URI string or an array of segments - * @return string - */ - public function site_url($uri = '') - { - $site_slug = SITE_SLUG.'/'; - - if($_SERVER['HTTP_HOST'] != 'localhost') - $site_slug = ''; - - if (empty($uri)) - { - return $this->slash_item('base_url').$this->item('index_page').$site_slug; - } - - $uri = $this->_uri_string($uri); - - if ($this->item('enable_query_strings') === FALSE) - { - $suffix = ($this->item('url_suffix') === FALSE) ? '' : $this->item('url_suffix'); - - if ($suffix !== '' && ($offset = strpos($uri, '?')) !== FALSE) - { - $uri = substr($uri, 0, $offset).$suffix.substr($uri, $offset); - } - else - { - $uri .= $suffix; - } - - return $this->slash_item('base_url').$this->slash_item('index_page').$site_slug.$uri; - } - elseif (strpos($uri, '?') === FALSE) - { - $uri = '?'.$uri; - } - - return $this->slash_item('base_url').$this->item('index_page').$site_slug.$uri; - } } \ No newline at end of file