在codeigniter中强制SSL

问题描述:

我已经搜索了网络,现在尝试了大多数东西,最近几个小时但没有结果。在codeigniter中强制SSL

我使用了CodeIgniter,我有一个。 Htaccess文件,我有一些规则。

我想强制SSL连接并从url中删除index.php。

这里是我的.htaccess

<IfModule mod_rewrite.c> 
    Options +FollowSymLinks 
    RewriteEngine On 
    RewriteBase/

    # Force SSL 
    RewriteCond %{HTTPS} off 
    RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L] 

    #remove ugly index.php 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ /index.php/$1 [L] 
</IfModule> 

<IfModule !mod_rewrite.c> 
    # If we don't have mod_rewrite installed, all 404's 
    # can be sent to index.php, and everything works as normal. 
    # Submitted by: ElliotHaughin 
    ErrorDocument 404 /index.php 
</IfModule> 
Options All -Indexes 

的问题是,这是行不通的。我在www.lotusmodellen.se上安装了证书,所有流量都必须到达那里。不lotusmodellen.se

请尽量进入这样http://www.lotusmodellen.sehttp://lotusmodellen.se

不管你写的,有或没有HTTPS,你应该来www.lotusmodellen.se

但它似乎是问题,重定向错误等等。因为它不起作用。

有没有人有解决方案?

嗨,只需在config.php文件中替换您的base_url,它将检查来自https的请求或http将根据此设置您的base_url。

if(isset($_SERVER['HTTP_HOST'])) 
{ 
    $config['base_url'] = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http'; 
    $config['base_url'] .= '://'. $_SERVER['HTTP_HOST']; 
    $config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']); 
} 

else 
{ 
    $config['base_url'] = 'http://localhost/'; 
}