Codeigniter redirect not preserving HTTPS in relative redirects Codeigniter redirect not preserving HTTPS in relative redirects codeigniter codeigniter

Codeigniter redirect not preserving HTTPS in relative redirects


Use this:

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

Instead of this:

$config['base_url'] = "http://www.example.com"

This'll always redirect to where you want it. You don't even have to enter your domain name, just use it as is!


In addition to the above, I also autoload this helper:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');function is_https_on(){    return isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on';}function use_ssl($turn_on = TRUE){    $url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];    if ( $turn_on )    {        if ( ! is_https_on() && $_SERVER['HTTP_HOST'] != 'localhost')        {            redirect('https://' . $url, 'location', 301 );            exit;        }    }    else    {        if ( is_https_on() )        {            redirect('http://' . $url, 'location', 301 );            exit;        }    }}/* End of file https_helper.php *//* Location: ./application/helpers/https_helper.php */

With this in place, I can set my individual pages to always use HTTPS/HTTP (either in a single method in my controller, or - if I want it to affect the whole controller - in the constructor).

I simply use:

use_ssl();

at the beginning of the script, to ascertain that it is loaded via HTTPS.

Alternatively, if I only want to serve it through HTTP, I'll use:

use_ssl(FALSE);


There is no 'out of the box' way to handle this HTTPS / HTTP handoff in CI. What you CAN do is create a small helper (that you auto include) which will add a function like secure_url(), and quite simply returns what a base_url would, but https format.

You would have to emulate the same redirect function maybe as secure_redirect().


you can use a protocol-relative url which will keep persistence in the linking in CI.

in your config.php instead of:

$config['base_url'] = 'http://example.com/';

put;

$config['base_url'] = '//example.com/';

information about protocol-relative links here.

i have tested this on ie11,chrom(e|ium),firefox,midori using CI form library and basic links. all the browsers honor the specification and i have seen this used in the wild without issue.

additionally CI version 3 has a is_https function built inyou should be able to simple use is_https in your controller (eg in the construct) and redirect if not https, then useing the above method maintain http or https without constant redirections.

eg, to keep the entire controller as https, insert into your controller:

public function __construct(){    parent::__construct();    $this->load->helper('url');    if(! is_https()){        redirect('https:'.base_url('/page'));    }    //!! $conig['base_url'] must be protocol-relative}