CodeIgniter Forums
From Http to Https - Now browser gives ERR_CONTENT_DECODING_FAILED error in console - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: From Http to Https - Now browser gives ERR_CONTENT_DECODING_FAILED error in console (/showthread.php?tid=64853)



From Http to Https - Now browser gives ERR_CONTENT_DECODING_FAILED error in console - MasoodRehman - 04-03-2016

Hello,

I have recently shift my site from http to https before shifting the site was working perfectly on http, now on https it gives errors like
one of my controller method echoing json response when browser request to that method it showing me the following error message in console, and i am using gzip for file compression.

    ERR_CONTENT_DECODING_FAILED

Some one says that it happens when your HTTP request's headers claim that the content is gzip encoded, but it isn't. Turn off gzip encoding setting or make sure the content is in fact encoded. So the following is request/response header


   


Currently i am handling routing through .htaccess, is there any method in codeigniter that automate my redirection process and change the base url according to the protocol from http to https and vice versa.

Thank you.


RE: From Http to Https - Tpojka - 04-03-2016

I saw topic where you find such an advice
Have you at first tried solutions from this and this answers? 
Try those if you didn't (in order I posted those). 
Also for a while, there is sort of common functions used in custom created helper like in this page:

PHP Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if (!
function_exists('force_ssl'))
{
 
   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());
 
       }
 
   }
}

function 
remove_ssl()
{
 
   $CI =& get_instance();
 
   $CI->config->config['base_url'] =
 
                 str_replace('https://''http://',
 
                 $CI->config->config['base_url']);
 
   if ($_SERVER['SERVER_PORT'] != 80)
 
   {
 
       redirect($CI->uri->uri_string());
 
   }
}
/* End of file ssl_helper.php */
/* Location: ./application/helpers/ssl_helper.php *//// 

PHP Code:
$autoload['helper'] = array('url''ssl'); 

Then, within `public function __construct()` of every controller requiring SSL, add the following:

PHP Code:
force_ssl(); 



But check first if you can solve it on server level.


RE: From Http to Https - MasoodRehman - 04-03-2016

Hi TPojka thanks for your reply,

I had tried those links before posting this thread here actually i overcome the issue as for as my issue was concern the error message was showing in the browser console is due to the json response from controller method to caller ajax method. I was echoing the response in controller method like the following

echo json_encode($data);

As i replace the above line with the following using proper output class of codeigniter, then the error resolved

$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($data));

I think there is issue between gzip compression on https and ajax method, by echoing json response it not properly working because i was using this method on http there were no issue.


Thanks once again for your reply.


RE: From Http to Https - MasoodRehman - 04-03-2016

But what if i have 10 to 30 controller or more i have to put manually force_ssl(); in each controller constructor,
I have found another solution for this by using hooks.

Just enable hooks from application/config/config.php

Put the following code into application/config/hooks.php

hooks.php

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

This hook will call the redirect_ssl function in ssl.php file located in application/hooks/ssl.php

ssl.php

function redirect_ssl()
{
$CI =& get_instance();

$class = $CI->router->fetch_class();

// Add more controller name to exclude ssl.
$exclude = array();

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

So this function will called immediately after your controller is instantiated, but prior to any method calls happening and set the base url according to the condition by default it will set https protocol for all controllers in case you want to exclude a controller from the ssl list just put the name of the class in $exclude array.

And you simply enable/disable the hooks from application/config/config.php file by just change the value of the $config['enable_hooks'] to true/false.

Thanks TPojka once again for your reply it was really useful for me.


RE: From Http to Https - Tpojka - 04-03-2016

Nice hooks or parent controller are good solution.
Still, that way you would have negative SEO implications. 
I'd check how to resolve it on Apache level before Google remember not wanted links.


RE: From Http to Https - MasoodRehman - 04-04-2016

Hello Tpojka,

I am facing another issue, the https issue has been resolved now there is another issue when i visit the site with www then it displaying the index.php in url but without www its url is just fine no index.php e.g

Case 1:

when i put www.example.com/mobiles in url and hit enter, then the url become like the following

https://www.example.com/index.php/mobiles

Case 2:

when i put just example.com/mobiles in url and hit enter, then the url become like

https://example.com/mobiles


Can we control this through .htaccess if yes then how?

Thanks.


RE: From Http to Https - Now browser gives ERR_CONTENT_DECODING_FAILED error in console - Tpojka - 04-04-2016

Could be issue with browser cache. 
Clear the cache (Ctrl+F5 on Windows) and see if anything better.


RE: From Http to Https - Now browser gives ERR_CONTENT_DECODING_FAILED error in console - InsiteFX - 04-04-2016

Create a MY_Controller and add the force_ssl in it

then all you have to do is extend the controller from it.

you can have more then one class in the MY_Controller class.


RE: From Http to Https - Now browser gives ERR_CONTENT_DECODING_FAILED error in console - MasoodRehman - 04-04-2016

(04-04-2016, 01:27 PM)Tpojka Wrote: Could be issue with browser cache. 
Clear the cache (Ctrl+F5 on Windows) and see if anything better.

Thanks Tpojka


RE: From Http to Https - Now browser gives ERR_CONTENT_DECODING_FAILED error in console - MasoodRehman - 04-04-2016

(04-04-2016, 04:35 PM)InsiteFX Wrote: Create a MY_Controller and add the force_ssl in it

then all you have to do is extend the controller from it.

you can have more then one class in the MY_Controller class.

Thanks InsiteFX.