0% found this document useful (0 votes)
17 views

Redirect To SSL in Codeigniter

1. The document discusses how to redirect to HTTPS URLs in CodeIgniter using hooks. 2. It explains that HTTPS provides secure data transmission using SSL certificates. CodeIgniter does not natively support HTTPS redirection. 3. Steps are provided to configure CodeIgniter hooks to redirect to HTTPS, including enabling hooks, creating a hooks.php file, and adding a redirect_ssl function to the ssl.php hooks file to replace HTTP with HTTPS in the base URL.

Uploaded by

Sani Jusouf
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Redirect To SSL in Codeigniter

1. The document discusses how to redirect to HTTPS URLs in CodeIgniter using hooks. 2. It explains that HTTPS provides secure data transmission using SSL certificates. CodeIgniter does not natively support HTTPS redirection. 3. Steps are provided to configure CodeIgniter hooks to redirect to HTTPS, including enabling hooks, creating a hooks.php file, and adding a redirect_ssl function to the ssl.php hooks file to replace HTTP with HTTPS in the base URL.

Uploaded by

Sani Jusouf
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Redirect to ssl in codeigniter :HTTPS :- Full name is (hyper text transfer protocol secure).

Its a extended or you can say secure


version of the HTTP(hyper text transfer protocol). If you have https enabled on your server you will
get a lock icon in address bar of browser.
For encryption of data its used SSL(secure sockets layer) certificates to install on your server. Most
uses of https to securely transmit data with ssl.
So most of sites need ssl or https url. we can easily manage redirection to https url in codeigniter
using hooks. its a one more good feature of CI. We need to create some lines of code to get ssl url
or redirect to https url in codeigniter. Default codeigniter not have this type of functionality so we need
to force redirect to ssl url. below we will see how to use ssl redirect with codeigniter.
1. Config changes :- Go to application/config/config.php and enable or set hooks to true.

$config['enable_hooks'] = TRUE;
2. create a new file named hooks.php in application/config/hooks.php and add below code in
hooks.php:-

$hook['post_controller_constructor'][] = array(
'function' => 'redirect_ssl',
'filename' => 'ssl.php',
'filepath' => 'hooks'
);
3. Now create a new directory with named hooks under application directory and then create new
file named ssl.php in application/hooks/ssl.php
and add below code to ssl.php :-

function redirect_ssl() {
$CI =& get_instance();
$class = $CI->router->fetch_class();
$exclude = array('client'); // add more controller name to exclude ssl.
if(!in_array($class,$exclude)) {
// redirecting to ssl.
$CI->config->config['base_url'] = str_replace('http://', 'https://', $CI>config->config['base_url']);
if ($_SERVER['SERVER_PORT'] != 443) redirect($CI->uri->uri_string());
}
else {
// redirecting with no ssl.
$CI->config->config['base_url'] = str_replace('https://', 'http://', $CI>config->config['base_url']);
if ($_SERVER['SERVER_PORT'] == 443) redirect($CI->uri->uri_string());
}
}

Now check your site you will get https in url. redirect to https url in codeigniter using hooks is a good
method rather than htaccess.

You might also like