如何在codeigniter中将http重定向到https

asi*_*han 16 php .htaccess codeigniter

点#1 如果我键入:

www.myurl.com/somepage
http://www.myurl.com/somepage
http://myurl.com/somepage
https://myurl.com/somepage
Run Code Online (Sandbox Code Playgroud)

让它重定向到

https://www.myurl.com/somepage
Run Code Online (Sandbox Code Playgroud)

第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
Run Code Online (Sandbox Code Playgroud)

来自评论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] 
Run Code Online (Sandbox Code Playgroud)

Pre*_*gde 22

请检查这可能会有所帮助

配置更改: - 转到"application/config/config.php"并启用或设置挂钩为true.

$config['enable_hooks'] = TRUE;
Run Code Online (Sandbox Code Playgroud)

在"application/config/hooks.php"中创建一个名为hooks.php的新文件,并在hooks.php中添加以下代码: -

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

现在在应用程序目录下创建一个名为"hooks"的新目录,然后在"application/hooks/ssl.php"中创建名为"ssl.php"的新文件,并将下面的代码添加到"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());
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 钩子是一个很好的方法而不是htaccess. (2认同)

小智 12

我尝试了所有的但是在我的情况下它们没有正常工作.我发现下面的解决方案,它适用于我的情况.我希望它对你也有帮助.

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]
Run Code Online (Sandbox Code Playgroud)


asi*_*han 6

现在我有了解决方案,我更新了htaccess文件。--

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]
Run Code Online (Sandbox Code Playgroud)

现在,它为我工作顺利.. :)


小智 5

将其插入 .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>
Run Code Online (Sandbox Code Playgroud)


小智 5

对于 Codeigniter 4,请遵循以下路径;

/应用程序/配置/应用程序.php

并将这一行更改为 true。

public $forceGlobalSecureRequests = false;
Run Code Online (Sandbox Code Playgroud)

它会直接重定向到 https。没有钩子,没有.htaccess。