【问题标题】:Codeigniter 2.x SSL Redirect LoopCodeigniter 2.x SSL 重定向循环
【发布时间】:2018-03-08 21:27:24
【问题描述】:

我正在开发一个预先存在的 codeigniter (v2.1.4) 应用程序以向其添加 https/ssl。

我在 webfaction 托管工作。他们通常的 ssl 设置方法包括在主机仪表板中添加第二条网站记录,该记录使用 .htaccess(为了清楚起见,我们称之为 .htaccess_B)文件将 http:// 重定向到 https://。此网站记录仅包含此 .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 证书。我已经尝试过this 方法(最佳答案),但我陷入了重定向循环。

我在 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 无济于事。我一直卡在重定向循环中

在 Chrome 中获取 https://mydomainhere.com/ net::ERR_TOO_MANY_REDIRECTS 控制台

有什么想法吗?

谢谢

【问题讨论】:

  • 尝试与他们开票。我不熟悉 PHP,但我上周在 webfaction 为 django 网站做了一个重定向。相同的方法 .htaccess 文件。他们检查并认为这是由于我的浏览器缓存造成的。清除缓存后,它起作用了。他们很有帮助。

标签: php .htaccess codeigniter ssl https


【解决方案1】:
  1. 更改配置中的 base_url:

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

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

【讨论】:

    猜你喜欢
    • 2017-04-10
    • 2014-04-24
    • 2015-02-26
    • 2015-02-15
    • 1970-01-01
    • 2014-06-01
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多