How to redirect http to https in codeigniter
Asked Answered
P

14

19

Point #1 If I type:

www.myurl.com/somepage
http://www.myurl.com/somepage
http://myurl.com/somepage
https://myurl.com/somepage

have it redirect to

https://www.myurl.com/somepage

Point #2

When I type in something like www.myurl.com it is redirecting to https://www.myurl.com/index.php.
Make it so the index.php is not displaying. It should just display https://www.myurl.com

From Comment htaccess

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^myhost\.com$ [NC] 
RewriteRule ^(.*)$ myhost.com/$1 [R=301,L] 
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php(/[^\ ]*)?\ HTTP/ 
RewriteRule ^index\.php(/(.*))?$ myhost.com/$2 [R=301,L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php/$1 [L] 
Phonate answered 4/1, 2016 at 7:12 Comment(6)
set this $route['404_override'] = '';Unrestraint
already set.but same issue occurePhonate
and set your htaccess tooUnrestraint
how to set in htaccess?Phonate
I already set in htaccess RewriteEngine On RewriteCond %{HTTP_HOST} ^myhost\.com$ [NC] RewriteRule ^(.*)$ myhost.com/$1 [R=301,L] RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php(/[^\ ]*)?\ HTTP/ RewriteRule ^index\.php(/(.*))?$ myhost.com/$2 [R=301,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] But the problem is..if i am type www.myhost.com then it redirect to www.myhost.com. i want to redirect myhost.com.Phonate
When you need to add code you can re edit your question by clicking on the edit button below tags.Tenant
P
11

Now I got a solution, I updated my htaccess file.--

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTP_HOST} ^myhost\.com$ [NC]
RewriteRule ^ https://www.myhost.com%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php(/[^\ ]*)?\ HTTP/ 
RewriteRule ^index\.php(/(.*))?$ myhost.com/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Now, It worked for me smoothly.. :)

Phonate answered 4/1, 2016 at 8:27 Comment(1)
firt time if i access link with http so does not work when i refresh page then it,s convert to https why in first access it,s not convert to https directlyErvin
C
27

I tried above all ones but they did not work properly in my case. I found below solution, it works in my case. I hope it will be helpful for you as well.

RewriteEngine on

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond $1 !^(index\.php|resources|robots\.txt|public)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
Cyrilcyrill answered 19/2, 2018 at 7:34 Comment(0)
T
25

Please Check this may help

Config changes :- Go to “application/config/config.php” and enable or set hooks to true.

$config['enable_hooks'] = TRUE;

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'
                                );

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());
    }
}
Transformer answered 4/1, 2016 at 8:49 Comment(4)
Can you give some arguments why using this php pratice is a better method than htaccess? They both check a certain rule every runUnearthly
For me, the SSL code detection did not work. Might be because we are using CloudFlare CDNto provide the SSL.Wylie
any of htaccess rewrite code didn't work for me! Hook worksAshmead
The best way is to use the htaccess approach because that is the first point of call to your server. Prevents any additional computations. Check this URL of how it works danielmorell.com/images/articles/htaccess_guide/…Turgite
P
11

Now I got a solution, I updated my htaccess file.--

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTP_HOST} ^myhost\.com$ [NC]
RewriteRule ^ https://www.myhost.com%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php(/[^\ ]*)?\ HTTP/ 
RewriteRule ^index\.php(/(.*))?$ myhost.com/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Now, It worked for me smoothly.. :)

Phonate answered 4/1, 2016 at 8:27 Comment(1)
firt time if i access link with http so does not work when i refresh page then it,s convert to https why in first access it,s not convert to https directlyErvin
A
7

Insert this into .htaccess

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

</IfModule>
Ancelin answered 26/3, 2020 at 15:37 Comment(0)
F
5

For Codeigniter 4, follow the path;

/app/config/app.php

and change this line to true.

public $forceGlobalSecureRequests = false;

it will redirect to https directly. no hook, no .htaccess.

Fertilize answered 8/10, 2020 at 4:41 Comment(0)
W
4

If you, specifically want to redirect, above answers are helpful. But if you are doing this because base is "HTTP", then,
redirecting is not correct method (as it asks for resources).
Instead, open "application/config/config.php" in your project and change the value of

$config['base_url']

Add "s" in the value
from

$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'];

to

$config['base_url'] = 'https://'.$_SERVER['HTTP_HOST'];
Will answered 9/9, 2020 at 12:40 Comment(2)
I added s in base_url but this , domaindotcom is not redirecting to httpsIndignant
manually delete the cached items in the "application/cache" folderWill
F
3

for me none of the above answer worked as it was showing me too many redirects but this one worked like charm. Hence I thought to share if someone else get into the similar issue:

RewriteEngine on

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (sale|success|cancel) [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !(static|sale|success|cancel) [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond $1 !^(index\.php|resources|robots\.txt|static) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Ferula answered 22/5, 2017 at 10:52 Comment(0)
R
1

First go to the application/config/config.php file and look for this line:

$config['base_url'] = "";

Set it to your URL with the https e.g. https://example.com

$config['base_url'] = "https://example.com";

Then go to your .htaccess file and add this line in the example given below.

RewriteRule ^(.*)$ https://example.com/$1 [R,L]

<IfModule mod_rewrite.c>

    #Options +FollowSymLinks
    #Options -Indexes
    RewriteEngine on

    RewriteCond %{HTTPS} off 
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

    # Send request via index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    RewriteRule ^(.*)$ https://example.com/$1 [R,L]
    

</IfModule>
Royalty answered 14/11, 2017 at 17:51 Comment(2)
Your first rewrite rule should not have the "[L]"Jugate
@Jugate It gave me a funny error and after a little read online I understood why, that is why I included itRoyalty
S
1

This work for me you can input it in your .htaccess just input at the last line

RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Sharasharai answered 21/3, 2020 at 7:1 Comment(0)
P
0

yes indeed this

   RewriteEngine On
   RewriteCond %{HTTPS} off [OR] 
   RewriteCond %{HTTP_HOST} !^www\. [OR]
   RewriteCond %{HTTP_HOST} ^myhost\.com$ [NC]
   RewriteRule ^ https://www.myhost.com%{REQUEST_URI} [R=301,L,NE]
   RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php(/[^\ ]*)?\ HTTP/ 
   RewriteRule ^index\.php(/(.*))?$ myhost.com/$2 [R=301,L]
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ index.php/$1 [L]

works but you have to make a slight edit. In the codeiginiterfolder/application/config.php file replace http with https.

Also, the base url in the above code you have to replace the myhost to your host name. Suppose my host name internetseekho.com so instead of myhost you do have write internetseekho.

Piave answered 6/4, 2017 at 6:8 Comment(0)
A
0

If you find the error "No Input file specified" then add "?" in the last line

RewriteRule ^(.*)$ index.php/?$1 [L]

in the answer from @asiya khan

Almucantar answered 8/7, 2017 at 5:46 Comment(0)
W
0

Our SSL is from the CloudFlare CDN and so the solutions above do not work.

The following SSL detection code (in SSL.php based on @Preetham Hegde solution) works:

$isSecure = false;
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
    $isSecure = true;
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') {
    $isSecure = true;
}
$REQUEST_PROTOCOL = $isSecure ? 'https' : 'http';


if ($REQUEST_PROTOCOL=="http")
{
    $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: ' . $redirect);
    exit();
}
Wylie answered 2/5, 2018 at 12:19 Comment(0)
S
0

Just add this line in your existing rules. It may work!

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Scornful answered 10/4, 2021 at 7:28 Comment(0)
P
0

This is working in Load balancer case where SSL cert in terminated at LB.

For Codeigniter 4, follow the path;

/app/config/app.php

and change this line to true.

public $forceGlobalSecureRequests = false; it will redirect to https directly. no hook, no .htaccess.

Postbox answered 13/5 at 21:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.