CodeIgniter 2.x SSL重定向循环



我正在研究预先存在的codeigniter(v2.1.4)应用程序,以将https/ssl添加到它。

我正在WebFaction托管工作。他们的常规SSL设置方法包括在主机仪表板中添加第二个网站记录,该记录将http://重定向到https://使用.htaccess(以清楚起见)。此网站记录仅包含此.htaccess文件:

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

包含CodeIgniter应用程序的原始网站记录包含此.htaccess(.htaccess_a)文件

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index.php|javascript|images|robots.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]

根据Webhost指令,安装并正确配置了SSL证书。我尝试了这种方法(最佳答案),但我陷入了重定向循环。

我的配置变量在config.php中:

$config['base_url'] = '';
$config['enable_hooks'] = TRUE;
$config['cookie_secure']    = TRUE;

hooks.php

$hook['post_controller_constructor'][] = array(
    'function' => 'redirect_ssl',
    'filename' => 'ssl.php',
    'filepath' => 'hooks'
    );

和我的ssl.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function redirect_ssl() {
    $CI =& get_instance();
    $class = $CI->router->fetch_class();
    $exclude =  array('');  // 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());
    }
}

`

我尝试了几件事,包括在原始网站记录(.htaccess_a)上重命名.htaccess文件,并添加一个带有https://的base_url。我一直陷入重定向循环

获取https://mydomainhere.com/net :: err_too_many_redirects in Chrome 控制台

有什么想法?

谢谢

  1. 在您的配置中更改base_url:

    $config['base_url'] = 'https://mydomainhere.com/';
    
  2. 从http重定向流量到https:

    RewriteCond %{HTTPS} off
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

最新更新