18

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] 
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
asiya khan
  • 311
  • 1
  • 2
  • 8
  • set this `$route['404_override'] = '';` – Abdulla Nilam Jan 04 '16 at 07:14
  • already set.but same issue occure – asiya khan Jan 04 '16 at 07:15
  • and set your htaccess too – Abdulla Nilam Jan 04 '16 at 07:16
  • how to set in htaccess? – asiya khan Jan 04 '16 at 07:32
  • I already set in htaccess RewriteEngine On RewriteCond %{HTTP_HOST} ^myhost\.com$ [NC] RewriteRule ^(.*)$ http://www.myhost.com/$1 [R=301,L] RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php(/[^\ ]*)?\ HTTP/ RewriteRule ^index\.php(/(.*))?$ http://www.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 https://www.myhost.com. – asiya khan Jan 04 '16 at 07:38
  • When you need to add code you can re edit your question by clicking on the edit button below tags. –  Jan 04 '16 at 07:40

13 Answers13

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]
Emrah Onder
  • 271
  • 1
  • 3
  • 4
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());
    }
}
Preetham Hegde
  • 839
  • 2
  • 13
  • 24
  • Can you give some arguments why using this php pratice is a better method than htaccess? They both check a certain rule every run – Nebulosar Jul 30 '17 at 20:23
  • 1
    For me, the SSL code detection did not work. Might be because we are using CloudFlare CDNto provide the SSL. – James May 02 '18 at 12:17
  • any of htaccess rewrite code didn't work for me! Hook works – yeshansachithak Feb 23 '19 at 13:49
  • 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 https://www.danielmorell.com/images/articles/htaccess_guide/daniel_morell_htaccess_rewrite_ruleset_processing_1.0.png – Nafiu Lawal May 26 '21 at 18:50
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.. :)

asiya khan
  • 311
  • 1
  • 2
  • 8
  • 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 directly – abubakkar tahir Aug 07 '20 at 05:55
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>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Rodrigo
  • 71
  • 1
  • 1
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.

MonkeyDLuffy
  • 556
  • 7
  • 17
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'];
sifr_dot_in
  • 3,153
  • 2
  • 33
  • 42
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]
Lonare
  • 3,581
  • 1
  • 41
  • 45
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>
Developer Kwame
  • 168
  • 1
  • 5
  • 14
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}
Dharman
  • 30,962
  • 25
  • 85
  • 135
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.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
I AM A Hacker
  • 113
  • 1
  • 1
  • 10
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

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();
}
James
  • 1,233
  • 3
  • 13
  • 18
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]
Syscall
  • 19,327
  • 10
  • 37
  • 52
Yagnesh bhalala
  • 1,107
  • 1
  • 15
  • 17