Subdomains leading to Codeigniter Controllers?

ぐ巨炮叔叔 提交于 2019-11-28 17:04:19

As you want to use a particular domain to lead to your controllers, what I came up with was using the application/config/routes.php file to achieve it. The idea is load different controllers depending on what subdomain you use, so, instead of writing a list of routes for your domain, you write a list of routes DEPENDING on the domain you're accessing from:

switch ( $_SERVER['HTTP_HOST'] ) {
    case 'students.mysite.com':
        $route['default_controller'] = "students";
    break;
    case 'teachers.mysite.com':
        $route['default_controller'] = "teachers";
    default:
        // The list of your $routes lines at is was...
    break;
}

To make this work, you only have to point the subdomain to your CI project (Dwayne Towell in the step 1 of the other answer explains how to do it perfectly) and you'll have everything working, your shared hosting won't be a problem and you won't have to configure the server.

Step 1: In CPanel, in Domains, in subdomains, add *.mysite.com (you only enter the * part) to redirect to /public_html/ (you enter nothing and/or delete wildcard) (or set this to whatever the current default value for www.mysite.com is currently.

Step 2 & 3: Use mod_rewrite to capture subdomain and move it to the "directory" part of the URL. I suspect it will be something like: (but I haven't tried it yet, RewriteLogLevel 9 is your friend)

RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^\.]+)\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/index.php/%1/$1 [L]

I also don't know if you can do the above using .htaccess. I have only done rewriting from httpd.conf.

In application/config/routes.php file, you need to write

$subDomains = array();
$subDomains['students.mysite.com'] = "student";
$subDomains['teachers.mysite.com'] = "teachers";

if(array_key_exists($_SERVER['HTTP_HOST'], $subDomains)) {
  $route['default_controller'] = $subDomains[$_SERVER['HTTP_HOST']];
}

!!!
A very important step you don't want to forget in addition to Chococroc's great example is to route any segments back to the subdomain controller otherwise you will end up routing to a controller that probably doesn't exist. Example using Chococroc's existing code

switch ( $_SERVER['HTTP_HOST'] ) {
    case 'students.mysite.com':
        $route['default_controller'] = "students";
    break;
    case 'teachers.mysite.com':
        $route['default_controller'] = "teachers";
    default:
        // The list of your $routes lines at is was...
    break;
}

Navigating to 'teachers.mysite.com/login' will load the 'login' controller NOT the expected 'teachers' controller.

If you don't want this unexpected behavior you need to route any segments back to the subdomain controller. They will now be a function of that controller.

switch ( $_SERVER['HTTP_HOST'] ) {
    case 'students.mysite.com':
        $route['default_controller'] = "students";
        $route['(:any)'] = "students/$1";
    break;
    case 'teachers.mysite.com':
        $route['default_controller'] = "teachers";
        $route['(:any)'] = "teachers/$1";
    default:
        // The list of your $routes lines at is was...
    break;
}

Navigating to 'teachers.mysite.com/login' will now load the 'teachers' controller and run the 'login' function within that controller.

There are other things you could enforce such as a subfolder for each domain, etc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!