How can I have CodeIgniter load specific pages using SSL? I have an apache2/mode_ssl
server. mod_ssl
uses a different document root than non-secure
Here's a good solution. http://sajjadhossain.com/2008/10/27/ssl-https-urls-and-codeigniter/
In application/config/config.php, set base_url to:
$config['base_url'] = ($_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http') . "://{$_SERVER['HTTP_HOST']}/";
This will allow it to work on any domain, which is convenient if you test locally.
This is what worked for me. Our server both have $_SERVER['SERVER_PORT'] and $_SERVER['HTTP_X_FORWARDED_PORT']
We should check first if $_SERVER['HTTP_X_FORWARDED_PORT'] is available and use this instead of $_SERVER['SERVER_PORT']
$config['base_url'] =
( ( ( empty($_SERVER['HTTP_X_FORWARDED_PORT']) ? $_SERVER['SERVER_PORT'] : $_SERVER['HTTP_X_FORWARDED_PORT'] ) == 443 ? 'https' : 'http') . "://" . $_SERVER['HTTP_HOST'] )
. str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']) ;
tested this on our linux server...
btw, it's based on skyler's answer before which was.
$config['base_url'] = ($_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http') . "://{$_SERVER['HTTP_HOST']}/";
the solution that came to my mind is to use str_replace() like so
$base_url_str = base_url();
$secure_base_url = str_replace("http","https",$base_url_str );
whenever I need a secure location , i use $secure_base_url instead of base_url(), so let's say your base_url() is http://www.example.com then $secure_base_url will be https://www.example.com