How to redirect http to https in codeigniter

2020-02-17 05:38发布

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] 

8条回答
淡お忘
2楼-- · 2020-02-17 06:14

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]
查看更多
迷人小祖宗
3楼-- · 2020-02-17 06:19

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.

查看更多
Lonely孤独者°
4楼-- · 2020-02-17 06:20

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

查看更多
We Are One
5楼-- · 2020-02-17 06:23

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

    # 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>
查看更多
爷、活的狠高调
6楼-- · 2020-02-17 06:27

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());
    }
}
查看更多
可以哭但决不认输i
7楼-- · 2020-02-17 06:28

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]
查看更多
登录 后发表回答