[英]How to configure Codeigniter for HTTPS (SSL)?
我有一個在普通 http 上開發和測試的 Codeigniter 應用程序。 現在 Apache 服務器已配置為所有頁面都存儲在啟用 SSL 的單個文件夾中。 SSL 證書已到位。 當我使用“ https://www ”瀏覽網站時,它會重定向到“ http://www ”。
注意:我不需要使用 SSL 加載特定頁面。 我只需要所有頁面都顯示為 HTTPS 。
我已經嘗試了 Stack Overflow 上建議的解決方案,包括:
$config['base_url'] = "https://www.yoursite.com/"
;當我同時嘗試上述方法時,Firefox 出現以下錯誤:
該頁面未正確重定向。 Firefox 檢測到服務器正在以一種永遠不會完成的方式重定向對該地址的請求。
許多 Stack Overflow 貢獻者建議這應該僅由 Apache 處理。 這對我來說很有意義,但是通過 .htaccess 強制使用 HTTPS 會產生同樣的錯誤。
有沒有一種簡單的方法可以在沒有鈎子和 SSL 助手等的情況下做到這一點?
這個答案有點(閱讀:很多)晚了,但你可以使用我做的這個鈎子(5 分鍾前),它會重定向到你的 HTTPS 網站並設置一些標題。
我將它與這個.htaccess
文件一起使用:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [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
設置$config['base_url'] = "https://www.yoursite.com/";
第2步
使用並編輯此 .htaccess 文件。
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
第一步: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]
要安裝在 Apache 服務器上的“mod_rewrite”模塊
最后:重啟 Apache 服務器。
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.