Codigniter 3索引方法表现异常

时间:2019-05-07 03:52:22

标签: php .htaccess codeigniter-3

我在子域上有一个CodeIgniter项目。现在,当我访问sub.example.com时,它会加载login page,并在成功登录后重定向到仪表板。登录并建立会话后,访问sub.example.com/login/会自动重定向到dashboard page。现在这是我的问题。成功登录后,访问sub.example.com不会重定向到任何地方,而只是加载登录页面。但是访问sub.example.com/index.php确实将我重定向到了仪表板页面。

由于某种原因,我的索引方法被调用或正常工作。

这是我的代码。

  

.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';
  

根控制器

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);
    }
  

更新

  1. 忘记提及我仅在远程服务器上遇到此问题。在localhost上运行正常。

5 个答案:

答案 0 :(得分:5)

她是一个主意-在加载会话对象后添加:

    if ($this->session->userdata("username") && $this->router->method == "index") {
            redirect(base_url("dashboard/"));
    }

答案 1 :(得分:2)

请使用此.htaccess

RewriteEngine On 
RewriteCond %{REQUEST_URI} ^/system.* 
RewriteRule ^(.*)$ index.php?/$1 [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.+)$ index.php?/$1 [L]

答案 2 :(得分:2)

如果您成功访问了sub.example.com/index.php,请尝试

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";
        }else{
            //go to dashboard function
            $this->index();
        }
    }
    $this->load->view('login', $data);
}

答案 3 :(得分:0)

原来,远程服务器上的文件存在问题。我不完全知道文件是否不同或已损坏,但是替换远程服务器上的整个项目可以解决问题

答案 4 :(得分:0)

“结果证明远程服务器上的文件存在一些问题。我不完全知道文件是否不同或已损坏,但是替换远程服务器上的整个项目解决了该问题”-

@Akash您应该比较本地和远程.htaccess文件。我认为这可以解释您的问题。