Php 如何为HTTPS(SSL)配置Codeigniter?
我有一个Codeigniter应用程序,它是在普通http上开发和测试的。现在Apache服务器的配置方式是将所有页面存储在一个启用SSL的文件夹中。SSL证书已就位。当我使用“”浏览网站时,它会重定向到“” 注意:我不需要使用SSL加载特定页面。我只需要将所有页面显示为HTTPS 我尝试了针对堆栈溢出的建议解决方案,包括:Php 如何为HTTPS(SSL)配置Codeigniter?,php,apache,.htaccess,codeigniter,ssl,Php,Apache,.htaccess,Codeigniter,Ssl,我有一个Codeigniter应用程序,它是在普通http上开发和测试的。现在Apache服务器的配置方式是将所有页面存储在一个启用SSL的文件夹中。SSL证书已就位。当我使用“”浏览网站时,它会重定向到“” 注意:我不需要使用SSL加载特定页面。我只需要将所有页面显示为HTTPS 我尝试了针对堆栈溢出的建议解决方案,包括: 修改.htaccess文件以强制HTTPS 设置$config['base\u url']=”https://www.yoursite.com/“ 当我同时尝试上述方法时,
$config['base\u url']=”https://www.yoursite.com/“
李>
当我同时尝试上述方法时,Firefox出现以下错误:
页面未正确重定向。Firefox检测到服务器正在以一种永远无法完成的方式重定向对此地址的请求
许多堆栈溢出贡献者建议,这应该只由Apache处理。这对我来说是有道理的,但是通过.htaccess强制使用HTTPS会产生同样的错误
没有钩子和SSL助手等,有没有一种简单的方法可以做到这一点?这个答案有点晚(阅读:很多),但你可以使用我制作的(5分钟前),它将重定向到你的HTTPS网站并设置一些标题
我将其与此.htaccess
文件一起使用:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L]
第一步
设置$config['base\u url']=”https://www.yoursite.com/";代码>
步骤2
使用并编辑此.htaccess文件
重新编写引擎打开
重写条件%{HTTPS}关闭
重写规则(.*)https://%{HTTP_HOST}%{REQUEST_URI}[R,L]
这很容易
转到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;
步骤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());
}
步骤2:application/configs/hooks.php
$hook['post_controller_constructor'][] = array(
'function' => 'force_ssl',
'filename' => 'ssl.php',
'filepath' => 'hooks'
);
步骤3:application/config/config.php
$config['enable_hooks'] = TRUE;
$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更改:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
要安装在Apache服务器上的“mod_rewrite”模块
最后:重新启动Apache服务器。CI\u app\application\config\config.php
$config['enable_hooks'] = TRUE;
$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>
重新启动发动机
#需要HTTPS
重写cond%{HTTPS}=在…上
重写规则(.*)https://%{HTTP_HOST}%{REQUEST_URI}[R,L]
#正在删除index.php文件-https://codeigniter.com/userguide3
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)$index.php/$1[L]
建议的解决方案是正确的,但在看到您使用的代码之前,我们无法帮助您。使用您迄今为止尝试的.htaccess
代码更新问题。htaccess代码如下所示:RewriteCond%{HTTPS}上的RewriteEngine=在重写规则^https://%%{HTTP_HOST}%{REQUEST_URI}[L,R=301]时,尝试使用RewriteCond%{SERVER_PORT}80
而不是RewriteCond%{https}=关于
。感谢您的建议,但使用该组合仍会出现相同的错误。Firefox:“页面重定向不正确。Firefox检测到服务器正在以一种永远不会完成的方式重定向对此地址的请求。”重新加载apache服务器并再次检查。