I have a CodeIgniter project on a subdomain. Now, When I visit sub.example.com it loads a login page and on successful login, it redirects to dashboard. Once logged in and session are in place visiting sub.example.com/login/ will auto-redirect to the dashboard page. Now here's my problem. After successful login, visiting sub.example.com doesn't redirect anywhere it simply load the login page. But visiting sub.example.com/index.php does redirect me to the dashboard page.
For some reason, my index method is called or working properly.
Here my code.
.htaccess
IndexIgnore *
php_value date.timezone Asia/Kolkata
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
## Remove www from URL
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://sub.example.com/$1 [R=301,L]
## Redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^sub.example.com$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
config.php
if ($_SERVER['REMOTE_ADDR'] == "127.0.0.1") {
    $config['base_url'] = "http://" . $_SERVER['SERVER_NAME'];
} else {
    $config['base_url'] = "https://" . $_SERVER['SERVER_NAME'];
}
routes.php
$route['default_controller'] = 'root';
$route['root_controller'] = 'root';
/*API CONTROLLER*/
$route['api_controller'] = 'api';
/*Guest Controller*/
$route['guest_controller'] = 'guest';
/*Custom Routes*/
$route['dashboard/(:any)'] = 'root/dashboard/$1';
$route['search/(:any)'] = 'root/search/$1';
$route['search/(:any)/(:num)'] = 'root/search/$1/$2';
$route['export/(:any)'] = 'root/export/$1';
root controller
public function __construct()
    {
        parent::__construct();
        $this->default_data = array("project_name" => PROJECT_NAME);
        if ($this->router->method == "privacy_policy") {
            return;
        }
        $this->load->library('session');
        if ($this->router->method == "login") {
            if ($this->session->userdata("username")) {
                redirect(base_url("dashboard/"));
            }
        } else {
            if (!$this->session->userdata("username")) {
                redirect(base_url("login/"));
            }
        }
        //Set MYSQL timezone
        $this->db->query("SET time_zone = '+05:30'");
    }
    /**
     * Dashboard View
     */
    public function index()
    {
        redirect(base_url("/dashboard/"));
    }
    /**
     * Login View
     */
    public function login()
    {
        $data = $this->default_data;
        if ($_POST) {
            $username = $this->input->post("username");
            $plain_password = $this->input->post("password");
            $this->load->model("authenticate");
            if (!$this->authenticate->auth($username, $plain_password)) {
                $data['message'] = "Invalid Credentials";
            }
        }
        $this->load->view('login', $data);
    }
Update
Hers is an idea - add this after you have loaded the session object:
    if ($this->session->userdata("username") && $this->router->method == "index") {
            redirect(base_url("dashboard/"));
    }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With