Force SSL in codeigniter

2019-08-23 07:00发布

问题:

I have searched the net and tried most things now, the last few hours but without result.

I use out of CodeIgniter and I have a. Htaccess file where I have some rules.

I want to force SSL connection and remove index.php from url.

Here is my .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

The problem is that it does not work. I have the certificate installed on www.lotusmodellen.se and all traffic must go there. Not to lotusmodellen.se

Please try to go into such http://www.lotusmodellen.se or http://lotusmodellen.se

Whatever you write, with or without https, you should come to www.lotusmodellen.se

But it seems to be problems, redirect error or something. For it does not work.

Does anyone have a solution?

回答1:

hi just replace your base_url in config.php file this will check either request from https or http will set your base_url according to that.

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/';
}