【问题标题】:How to configure Codeigniter for HTTPS (SSL)?如何为 HTTPS (SSL) 配置 Codeigniter?
【发布时间】:2016-05-01 15:06:58
【问题描述】:

我有一个在普通 http 上开发和测试的 Codeigniter 应用程序。现在 Apache 服务器已配置为所有页面都存储在启用 SSL 的单个文件夹中。 SSL 证书已就位。当我使用“https://www”浏览网站时,它会重定向到“http://www”。

注意:我不需要使用 SSL 加载特定页面。我只需要将所有页面显示为 HTTPS

我已经尝试过 Stack Overflow 上建议的解决方案,包括:

  1. 修改 .htaccess 文件以强制使用 HTTPS
  2. 设置$config['base_url'] = "https://www.yoursite.com/";

当我同时尝试上述方法时,Firefox 给出了以下错误:

页面未正确重定向。 Firefox 检测到服务器正在以永远不会完成的方式重定向对该地址的请求。

许多 Stack Overflow 贡献者建议这应仅由 Apache 处理。这对我来说很有意义,但通过 .htaccess 强制 HTTPS 会产生同样的错误。

有没有不使用钩子和 SSL 助手等的简单方法?

【问题讨论】:

  • 建议的解决方案是正确的,但在我们看到您正在使用的代码之前,我们无法帮助您。使用您目前尝试过的.htaccess 代码更新问题。
  • .htaccess 代码如下: RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  • 尝试使用RewriteCond %{SERVER_PORT} 80 而不是RewriteCond %{HTTPS} !=on
  • 感谢您的建议,但使用该组合我仍然遇到相同的错误。 Firefox :“页面未正确重定向。Firefox 检测到服务器正在以永远不会完成的方式重定向对该地址的请求。”
  • 重新加载apache服务器并再次检查。

标签: php apache .htaccess codeigniter ssl


【解决方案1】:

这个答案有点晚了(阅读:很多),但您可以使用我(5 分钟前)制作的 hook,它会重定向到您的 HTTPS 网站并设置一些标头。

我将它与这个 .htaccess 文件一起使用:

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

【讨论】:

    【解决方案2】:

    很简单,

    转到 applicaton/config.php 并插入以下代码:

    $base  = "https://".$_SERVER['HTTP_HOST']; // (For https)
    

    参见图片上的第 19 行。

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    $base  = "https://".$_SERVER['HTTP_HOST']; // HERE THE CORRECT CODE
    $base .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
    $config['base_url'] = $base;
    

    【讨论】:

      【解决方案3】:

      第一步

      设置$config['base_url'] = "https://www.yoursite.com/";

      第二步

      使用和编辑这个 .htaccess 文件。

      RewriteEngine On
      RewriteCond %{HTTPS} off
      RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
      

      【讨论】:

        【解决方案4】:

        第1步:application/hooks/ssl.php

        function force_ssl()
        {
            $CI =& get_instance();
            $CI->config->config['base_url'] = str_replace('http://', 'https://', $CI->config->config['base_url']);
            if ($_SERVER['SERVER_PORT'] != 443) redirect($CI->uri->uri_string());
        }
        

        第二步:application/configs/hooks.php

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

        第三步:application/config/config.php

        $config['enable_hooks'] = TRUE;
        

        htaccess 变化:

        RewriteCond %{SERVER_PORT} 80 
        RewriteRule ^(.*)$ https://example.com/$1 [R,L]
        

        “mod_rewrite”模块将安装在您的 Apache 服务器上

        最后:重启 Apache 服务器。

        【讨论】:

          【解决方案5】:

          CI_app\application\config\config.php

          $base_url = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
          $base_url .= "://". @$_SERVER['HTTP_HOST'];
          $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
          
          $config['base_url'] = $base_url;
          

          .htaccess

          <IfModule mod_rewrite.c>
          RewriteEngine On
          
          # Require HTTPS
          RewriteCond %{HTTPS} !=on
          RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
          
          # Removing the index.php file - https://codeigniter.com/userguide3
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^(.*)$ index.php/$1 [L]
          </IfModule>
          

          【讨论】:

            猜你喜欢
            • 2015-08-03
            • 2018-12-18
            • 1970-01-01
            • 2019-03-10
            • 2013-05-11
            • 2012-06-17
            • 1970-01-01
            • 2016-02-26
            • 1970-01-01
            相关资源
            最近更新 更多