Search code examples
php.htaccesscodeignitersubdomaincpanel

Codeigniter Subdomain Redirect - Controller


I want to be directed to the controller that I specified when I logged in with the subdomain.

for example

www.mysite.com -> mysite.com/main_controller

abc.mysite.com -> mysite.com/sub_controller
stack.mysite.com -> mysite.com/sub_controller
test.mysite.com -> mysite.com/sub_controller

www.mysite.com/other_controller -> mysite.com/other_controller

How can I direct subdomains like this?

Thank you.


Solution

  • You don't need to play with htaccess, we can do some tricks. find below.

    Create new folder inside the root folder (where you have installed the application and system folders).

    Here i'm calling subdomain is the new folder for this case. So your root directory should like this.

    /application
    /system
    /subdomain
    /.htaccess
    /index.php
    /..
    

    Then copy /index.php & paste it inside the /subdomain directory and change sub settings like below

    $system_path = '../system';
    $application_folder = '../application';
    $view_folder = '../application/views';
    

    then, add this code in your /application/config/routes.php

    if($_SERVER['HTTP_HOST'] == 'mysite.com'){
        $route['default_controller'] = 'main_controller';   
    }else{
        $route['default_controller'] = 'sub_controller'; 
    }
    

    -OR-

    switch ( $_SERVER['HTTP_HOST'] ) {
        case 'abc.mysite.com':
            $route['default_controller'] = "sub_controller";
        break;
        case 'stack.mysite.com':
        case 'test.mysite.com':
            $route['default_controller'] = "sub_controller_2";
        break;
        default:
            $route['default_controller'] = 'main_controller';
    }
    

    finally, point your subdomain (ex. abc.mysite.com) to /subdomain directory

    you do it with other method too, if you need refer following links. https://code.tutsplus.com/tutorials/basecamp-style-subdomains-with-codeigniter--net-16330 http://asvignesh.in/dynamic-subdomain-in-codeigniter/