简体   繁体   中英

CodeIgniter Subdomain Routing Best Practice

I am soon to redevelop a site and I am wondering what the best practice is for routing CodeIgniter for subdomains. My site will have two "sections", a normal user section and a business section. The user section will live at http://example.com whilst the business section will live at http://business.example.com . Currently to route this I am checking for the domain in the routes file and using a different set of routes for each, something on the lines of:

$url = explode('http://', $_SERVER['HTTP_HOST']);

if($url[0] == 'business.example.com') {
  // routes for the "business" section
  $route['default_controller'] = 'business/homepage/index';
} else {
  // all other routes
  $route['default_controller'] = 'users/homepage/index';
}

I have also split my controllers up into two main folders, "business" and "users".

I am just wondering if this is actually the best way to achieve the desired routing in CodeIgniter or if anyone else can suggest a better approach.

You can use two "application" folders for each purpose and switch the applications in the index.php file.

Here is the explanation on the official site:

https://ellislab.com/codeigniter/user-guide/general/managing_apps.html

Mr. Taha's above answer is a good solution, but i have developed this with a kind of different method in ci.

In both of your folders (the domain and subdomain) in your server you must have the index.php and the .htaccess file. You must define your paths to application and system folders of course and you can define a var (like your domain or something usefull for you) which you will use it later everywhere.

In your app you can use a _DOMAIN define in index.php for the https://business.example.com domain something like:

define('_DOMAIN', 'https://business.example.com');

With this _DOMAIN var you can handle routings like:

    if (_DOMAIN == 'https://business.example.com') {
    $route['default_controller'] = 'Your/Controller';
}

and almost everything you want or where you want it (Controllers,libraries,etc..).

Don't know if this is the best solution but for me it was SUPER! Hope i helped

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM