Have you tried using the Codeigniter route configuration?
you do not need to use htaccess rewrite - although its a valid approach, you can just check the subdomain in the config/route.php and set the routing for your subdomains.
switch ($_SERVER['HTTP_HOST']) { case 'admin.domain.com': $route['(:any)'] = "admin/$1"; // this will set any uri and add the controler fodler to it $route['default_controller'] = "admin/home"; // set the default controller for this subdomain break; case 'api.domain.com': $route['(:any)'] = "api/$1"; // this will set any uri and add the controler fodler to it $route['default_controller'] = "api/home"; // set the default controller for this subdomain break; }
If you want this to be more general / dynamic routing, you can get it like this (in the same config/route.php ):
$controllerFolderName = array_shift((explode(".",$_SERVER['HTTP_HOST']))); $route['(:any)'] = $controllerFolderName."/$1"; $route['default_controller'] = $controllerFolderName."/home";
this routing will work for all subdomains and will set the default routing to a folder inside the controller folder with the same name as the subdomain, so for a domain such as api.domain.com, you will have a route set to api, etc. ..
it is important that you keep the same logic for all folder names that will always correspond to your subdomain, and I also suggest adding an error handling system for visitors without a subdomain ( http://domain.com ), and for cases when you have a subdomain, but a folder with this name does not exist (you can do this with file_exits )
Lupin source share