【问题标题】:Redirect loop in CI after installing SSL Certificate安装 SSL 证书后 CI 中的重定向循环
【发布时间】:2015-01-08 23:41:21
【问题描述】:

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

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

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

网站是使用 php 的 codeigniter 框架构建的。
请帮忙。

我将此代码添加到我的 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>

【问题讨论】:

  • 加上httpd.conf代码就更容易理解了...
  • 我已经添加了代码...如果你发现了什么请告诉我

标签: apache .htaccess codeigniter ssl https


【解决方案1】:
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());
    }
}

【讨论】:

    猜你喜欢
    • 2020-02-03
    • 2016-02-02
    • 2016-08-30
    • 2013-11-15
    • 2015-08-20
    • 2017-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多