CodeIgniter - redirect subdomains set to folder in "controllers" CodeIgniter - redirect subdomains set to folder in "controllers" codeigniter codeigniter

CodeIgniter - redirect subdomains set to folder in "controllers"


Did you try using the route configuration of Codeigniter?

you don't have to use htaccess rewrite - although its a valid approach, you can just check for the subdomain in the config/route.php file 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 would like it to be a more generic/ dynamic routing, you can have it like this (in the same config/route.php file):

$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 like api.domain.com you will have the route set to api etc.

it is important that you keep the same logic for all folder name that they will always match your subdomain and i would also suggest to add an error handling system for visitors with no subdomain (http://domain.com) and for cases where you have the subdomain but a folder with that name does not exists (you can do that with file_exits)


After a few more hours of digging I think I solved the problem. Here's how (for the ones that care):

# Subdomains to Folders + Enforce wwwRewriteCond %{HTTP_HOST} ^(www|admin|api) [NC]RewriteRule ^(.*)$ http://www.localhost/%1/$1 [L,P,S=1]RewriteRule ^(.*)$ http://www.localhost/$1 [L,R=301]

I combined the internal redirect with the www enforcer rule. All there is to do after this is configure the Apache Server to accept and properly redirect the PROXY request :)

Have phun!


Tried the suggestions above but ran into a lot of issues. Found a solution that allows you to route all URIs to a directory based on the subdomain, while allowing you to use the codeigniter routing functionality as it was intended.

First, place the follwing code in your application/core folder:

class MY_Router extends CI_Router {    public function __construct($routing=NULL) {        $routing['directory'] = explode('.',$_SERVER['HTTP_HOST'])[0];        parent::__construct($routing);     }}

Codeigniter will now place you subdomain directory before all controllers when routing. (i.e. ...application/subdomain_dir/class/method)

Next, set your base_url in the config.php file.

$subdomain = explode('.',$_SERVER['HTTP_HOST'])[0];$config['base_url'] = 'http://'.$subdomain.'.domain.com';

Finally, use the routes.php as it was intended.

$route['default_controller'] = "home";

The default controller above will now be routed to subdomain_dir/home. Similarly, if you navigate to subdomain.domain.com/class/method, you will be routed to subdomain_dir/class/method.

Hope this helps someone in the future.