How can I have CodeIgniter load specific pages using SSL? I have an apache2/mode_ssl
server. mod_ssl
uses a different document root than non-secure pages. For example, https (port 443) would serve pages out of /var/www/ssl_html/
And http (port 80) serves pages out of /var/www/html/
. How would I get CodeIgniter to play nice with this setup?
I have a Codeigniter application which was developed and tested on the normal http. Now the Apache server has been configured in such a way that all of the pages are stored in a single folder which is SSL enabled. The SSL certificate is in place. When I browse to the website using "https://www" then it redirects to "http://www".
CodeIgniter has its own command that you can use though. will start a web server, accessible on port 8080. If you set the location field in your browser to localhost:8080, you should see the CodeIgniter welcome page. You can now try several URLs in the browser location field, to see what the Pages controller you made above produces…
This means that the new Pages class can access the methods and variables defined in the CodeIgniter\Controller class ( system/Controller.php ). The controller is what will become the center of every request to your web application.
This tutorial assumes you’ve downloaded CodeIgniter and installed the framework in your development environment. The first thing you’re going to do is set up a controller to handle static pages. A controller is simply a class that helps delegate work. It is the glue of your web application. For example, when a call is made to:
There are few ways to tackle this.
Option 1:
I would probably have the code deployed to both folders, then in the file: /system/application/config/config.php, set your page to:
$config['base_url'] = "http://www.yoursite.com/";
or
$config['base_url'] = "https://www.yoursite.com/";
Then in your non-ssl VirtualHost folder, set your config to redirect protected pages by folder to the SSL site:
RedirectPermanent /sslfolder https://www.yoursite.com/sslfolder
Option 2:
Send everything to SSL and keep all your code in one folder
/system/application/config/config.php, set your page to:
$config['base_url'] = "https://www.yoursite.com/";
Other Options
There are some more hacky ways to do this with header() redirects, etc. but I don't think you want to maintain different code bases for this option. I don't recommend this but you could do something like:
$config['base_url'] = “http://” . $_SERVER['http_host'] . “/”;
In application/config/config.php, set base_url to:
$config['base_url'] = ($_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http') . "://{$_SERVER['HTTP_HOST']}/";
This will allow it to work on any domain, which is convenient if you test locally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With