Redirect http to https in codeigniter

Follow below steps to redirect you non-http site to https without using .htaccess

  • Change Configuration: go to application/config/config.php and enable hooks to true
            $config['enable_hooks'] = TRUE;
  • Create new file hooks.php if not exist other wise open file and paste code
          $hook['post_controller_constructor'][] = array(
                                'function' => 'redirect_ssl',
                                'filename' => 'ssl.php',
                                'filepath' => 'hooks'
                                );
  • Now create a new folder hooks if not exist under application folder and then create new file ssl.php and add below code to ssl.php
 <?php
function redirect_ssl() {
    $CI =& get_instance();
    $class = $CI->router->fetch_class();
    $exclude =  array('client');
    if(!in_array($class,$exclude)) {
      $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 {
      $CI->config->config['base_url'] = str_replace('https://', 'http://', $CI->config->config['base_url']);
      if ($_SERVER['SERVER_PORT'] == 443) redirect($CI->uri->uri_string());
    }
}
?>

Comments

Popular posts from this blog

Stop video playing when Bootstrap modal is closed