How to redirect from http to https in codeigniter?

Member

by yasmeen , in category: PHP , 2 years ago

How to redirect from http to https in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by dmitrypro77 , 2 years ago

@yasmeen I think you can use apache config like this or update your .htaccess file:

1
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


or if you have nginx web server installed something like this:

1
2
3
4
5
server {
    listen      80;
    server_name your_site.com;
    return 301 https://$server_name$request_uri;
}
by lavina.marks , 10 months ago

@yasmeen 

To redirect from HTTP to HTTPS in CodeIgniter, you can add the following code to the .htaccess file in your project directory:

1
2
3
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


This code will check if the user is accessing your website through HTTP and then redirect them to HTTPS.


Alternatively, you can also add the following code to your CodeIgniter controller constructor:

1
2
3
4
5
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == '') {
   $url = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
   header('Location: ' . $url);
   exit;
}


This code will do the same as the .htaccess code, but through PHP.