安装SSL证书后重定向CI中的循环

时间:2014-11-12 07:37:35

标签: apache .htaccess codeigniter ssl https

我刚刚在我的网站上安装了ssl证书,将URL从http://更改为https://一切都已完成,我还在httpd.conf文件中添加了一个代码,自动将https://添加到UR所以连接始终是安全的。

但是当我尝试登录管理面板时,我遇到了问题。它进入重定向循环,网页给出了"This webpage has a redirect loop"错误。

https://mysite.com加载正常,但https:/mysite.com/admin进入重定向循环。

使用codeigniter Framework为php构建站点 请帮忙。

我将此代码添加到我的httpd.conf文件

#
# Redirect http Request to https
# The lines below are used to redirect http request to https
#
<IfModule mod_rewrite.c>
     RewriteEngine On
     RewriteCond %{HTTPS} off
     RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>

1 个答案:

答案 0 :(得分:0)

Open config file from location application/config/config.php and enable or set hooks to true like this:

$config['enable_hooks'] = TRUE;
Then create a new file named hooks.php inside the config folder (i.e. application/config/hooks.php) and add the following code in it:

$hook['post_controller_constructor'][] = array(
    'function' => 'redirect_ssl',
    'filename' => 'ssl.php',
    'filepath' => 'hooks'
);
Now create a new directory named hooks inside the application folder (i.e. application/hooks) and then create a new file named ssl.php inside the hooks folder (i.e. application/hooks/ssl.php).

Add the following code in the ssl.php file:

function redirect_ssl() {
    $CI =& get_instance();
    $class = $CI->router->fetch_class();
    $exclude =  array('client');  // add more controller name to exclude ssl.
    if(!in_array($class,$exclude)) {
        // redirecting to ssl.
        $CI->config->config['base_url'] = str_replace('http://', 'https://', $CI->config->config['base_url']);
        if ($_SERVER['SERVER_PORT'] != 443) redirect($CI->uri->uri_string());
    } else {
        // redirecting with no ssl.
        $CI->config->config['base_url'] = str_replace('https://', 'http://', $CI->config->config['base_url']);
        if ($_SERVER['SERVER_PORT'] == 443) redirect($CI->uri->uri_string());
    }
}