简体   繁体   中英

How to configure Codeigniter for HTTPS (SSL)?

I have a Codeigniter application which was developed and tested on the normal http. Now the Apache server has been configured in such a way that all of the pages are stored in a single folder which is SSL enabled. The SSL certificate is in place. When I browse to the website using " https://www " then it redirects to " http://www ".

NOTE: I do not need to load specific pages using SSL. I just need all pages to show as HTTPS .

I have tried the proposed solutions suggested on Stack Overflow including:

  1. Modifying the .htaccess file to force HTTPS
  2. Setting the $config['base_url'] = "https://www.yoursite.com/" ;

When I tried the above method in tandem then Firefox gave the following error:

The page isn't redirecting properly. Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

Many Stack Overflow contributors suggest that this should be handled by Apache only. That makes sense to me but forcing HTTPS via .htaccess gives the same error.

Is there a simple way to do this without hooks and SSL Helpers, etc?

This answer is a bit (read: a lot) late, but you can use this hook I made (5 minutes ago), It'll redirect to your HTTPS website and set some headers.

I use it along with this .htaccess file:

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

It is easy,

Go to applicaton/config.php and insert the following code:

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

See line 19 on image.

<?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;

看看这个

Step 1

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

Step 2

Use and edit this .htaccess file.

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

Step 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());
}

Step 2: application/configs/hooks.php

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

Step 3:application/config/config.php

$config['enable_hooks'] = TRUE;

htaccess Changes:

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

'mod_rewrite' module to be installed on your Apache server

Final: restart Apache server.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM