如何在CodeIgniter中将HTTP重定向到HTTPS

18

第一点 如果我输入:

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

将其重定向到

https://www.myurl.com/somepage

第二点

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

来自评论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] 

将此代码 $route['404_override'] = ''; 设置为: - Abdulla Nilam
已经设置,但是出现了相同的问题。 - asiya khan
并设置您的htaccess。 - Abdulla Nilam
如何在htaccess中进行设置? - asiya khan
我已经在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]但问题是,如果我输入www.myhost.com,则会重定向到www.myhost.com。我想要重定向到https://www.myhost.com。 - asiya khan
当您需要添加代码时,可以通过单击标签下方的编辑按钮重新编辑您的问题。 - user4419336
13个回答

27

我尝试过以上所有方法,但在我的情况下它们都无法正常工作。我找到了以下解决方案,它在我的情况下有效。希望它对你也有帮助。

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]

25

请检查,这可能会有所帮助

配置更改:转到“application/config/config.php”,将钩子启用或设置为true。

$config['enable_hooks'] = TRUE;

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

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

现在在应用程序目录下创建一个名为“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());
    }
}

你能给出一些理由,说明为什么使用这种 PHP 实践方法比使用 htaccess 更好吗?它们都会在每次运行时检查某个规则。 - Nebulosar
1
对我来说,SSL代码检测没有起作用。可能是因为我们使用CloudFlare CDN提供SSL。 - James
任何htaccess重写代码都对我无效!钩子起作用了。 - yeshansachithak
最好的方法是使用htaccess方法,因为这是服务器的第一个调用点。可以防止任何额外的计算。请查看以下网址以了解其工作原理:https://www.danielmorell.com/images/articles/htaccess_guide/daniel_morell_htaccess_rewrite_ruleset_processing_1.0.png - Nafiu Lawal

11

现在我有一个解决方案了,我更新了我的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]

现在,它对我来说很顺利.. :)


第一次访问使用http链接时可能会无法工作,但当我刷新页面时,它会自动转换成https。为什么第一次访问不能直接转换成https? - abubakkar tahir

7
将以下内容插入到 .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>

5

针对Codeigniter 4版本,请按以下路径进行操作:

/app/config/app.php

将其中一行更改为true。

public $forceGlobalSecureRequests = false;

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


4
如果你想要进行特定的重定向,上面的回答是有帮助的。 但如果你这样做是因为基础是"HTTP",那么重定向不是正确的方法(因为它会请求资源)。 相反,打开你项目中的"application/config/config.php"文件并修改其值。
$config['base_url']

在值中添加“s”
来自
$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'];

to

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

我在base_url中添加了“s”,但是这个"http://domaindotcom/"没有重定向到https。 - Hamza Zafeer
手动删除“application/cache”文件夹中的缓存项。 - sifr_dot_in

3
对我来说,以上答案都没有起作用,因为它向我显示了太多的重定向,但是这个方法非常有效。因此,我想分享一下,如果有人遇到类似的问题:
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]

1
这对我很有帮助,您可以将其输入到您的.htaccess文件中,只需在最后一行输入即可。
RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

1
首先打开 application/config/config.php 文件,找到这一行:
$config['base_url'] = "";

将其设置为您的URL,使用https,例如https://example.com
$config['base_url'] = "https://example.com";

接着进入你的.htaccess文件,在下面的示例中添加以下行。

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>

你的第一条重写规则不应该有"[L]"。 - Risteard
@Risteard 它给了我一个有趣的错误,经过一些在线阅读后,我明白了原因,这就是为什么我把它包含在内的原因。 - Developer Kwame

0
如果您遇到错误"未指定输入文件",则在最后一行添加"?"。

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

答案来自@asiya khan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接