skip to Main Content

I have a website build with Codeigniter HMVC. On my local machine using Wamp it runs like it should. I want to upload it to my webserver that has an SSL certificate.

No matter what i try, i cant get the website to run on my webserver. I have tried following this :
How to force ssl in codeigniter?
But none of the given solutions work in my situation.

my .htaccess file looks like this :

Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index.php|images|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

in config.php of codeigniter i have :

$config['base_url'] = 'https://www.example.com/';

my webserver is an ubuntu 16 machine with apache, using an letsencrypt certificate. mod rewrite is enabled, but besides that is a standard configuration.

I have tried redirecting with hooks as mentioned in the link above, but to no avail. I have also tried various .htaccess file configuration but also no result there.

Is there anyone who has this running on their server?

regards,

sander

2

Answers


  1. You must add after RewriteEngine on

    <IfModule mod_ssl.c>
        RewriteCond %{HTTPS} off
        RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    </IfModule>
    
    Login or Signup to reply.
  2. Because you say,

    my webserver is an ubuntu 16 machine

    I assume you are not using a shared server from a hosting provider and so you have complete access to Apache’s configuration files. If that is true then you should not use .htaccess files. Read THIS and THIS to learn why.

    I also assume you have a set of Apache “sites-available” files that define <VirtualHost> configs. It is inside those <VirtualHost> blocks where you should be placing the code you currently have in .htaccess.

    To redirect all http requests to https try this as the contents of the “default” site in a “sites-available” configuration file e.g. “000-default.conf”

    <VirtualHost *:80>
        ServerName www.example.com
        ServerAlias example.com
        RedirectMatch 301 (.*) https://www.example.com$1
    </VirtualHost>
    

    What this will do is take any http request and immediately redirect to the https: URL. If you are using some other port besides the typical :80, adjust accordingly.

    As mentioned, you can do the rewriting in a VirtualHost. Here’s an example for the https: config file (maybe named along the lines of “020-example-443.conf”)

    <VirtualHost *:443>
        ServerName www.example.com
        ServerAlias example.com
        DocumentRoot /var/www/whatever
        ServerAdmin [email protected]
    
        <Directory /var/www/whatever>
            DirectoryIndex index.php
            Options -Indexes +FollowSymLinks
            AllowOverride All
            Require all granted
    
            RewriteEngine on
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.*)$ index.php?/$1 [PT]
        </Directory>
    
        #   SSL Engine Switch:
        #   Enable/Disable SSL for this virtual host.
        SSLEngine on
    
        # Certificates delivered by certbot - your's maybe elsewhere
        SSLCertificateFile  /etc/letsencrypt/live/yoursite/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/yoursite/privkey.pem
    
        #SSLOptions +FakeBasicAuth +ExportCertData +StrictRequire
        <FilesMatch ".(cgi|shtml|phtml|php)$">
        SSLOptions +StdEnvVars
        </FilesMatch>
    
        <Directory /usr/lib/cgi-bin>
            SSLOptions +StdEnvVars
        </Directory>
    </VirtualHost>
    

    With these in place, you won’t have to do anything special in CodeIgniter except, as you already do, use the following

    $config['base_url'] = 'https://www.example.com/';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search