codeigniter folder structure with SSL codeigniter folder structure with SSL codeigniter codeigniter

codeigniter folder structure with SSL


OK the best way to split things up the way you want, would be to:

  1. Set up your CodeIgniter app under a folder, say /var/www and ensure everything is working as you want.
  2. Set the base url for the site under the config.php of CodeIgniter to just "/".
  3. Create an Apache virtual host for the secure portion of the site, listening to requests on port 443 or whatever. Install your certificate and so on. http://www.namecheap.com are good for certs. Set up the web root as the CodeIgniter folder, e.g. /var/www.
  4. Create a further Apache virtual host, pointing to the same directory e.g. /var/www for the unsecure version of the website.

You will now, all being well, at this stage be able to access the entire site using either https or standard http. I think you mentioned being able to take things a step further by only allowing access to certain controllers via HTTPS and certain unsecure. What I would do for this is the following.

  1. Create a CodeIgniter library, call it say Ssl.php, under your application/libraries folder. Put in the following code:

class Ssl {    public function require()    {        // Is the current request method secure, via SSL?        if ( ! isset($_SERVER['https']) )        {            // No. Do something here, display an error, redirect... up to you            show_error("This resource must be accessed through an SSL encrypted connection.");        }    }}

Now, in your application controllers, simply load the library the usual way $this->load->library('ssl') and for any controller method that you wish to require an SSL connection for, simply call the $this->ssl->require() method before any execution starts.

You could even go a step further and drop that method call to require() in a class controller __construct() function, or even an entire new controller that you may wish to extend from.

I hope this helps in some way.


Hey, I am in the middle of developing a CI app that is successfully running with HTTPS/SSL.

I think you are a bit confused. As far as I know, you can only set up an SSL enabled site per se by creating a new site or "virtual host" if you are using Apache for example.

So essentially if you were using Apache, you would create a virtual host to handle requests on port 443 for say https://example.com and then set the web root to say /var/www or wherever your CI app sits. You would also have to configure Apache to use your certificate file, once you have bought the cert and downloaded the bits and bobs after generating the cert request. It's easier than it sounds.

Is there any reason why you can't just have your entire app running through SSL? Rather than an encrypted and non-encrypted section? There is a small CPU overhead for SSL but it is minimal.

I hope this helps in one way or another.

EDIT IN RESPONSE TO COMMENT:

You're welcome. It's a minimal overhead. For the hassle, I would just simple put it all under an SSL vhost. Plus, if you were to split content between SSL/non-SSL, you may notice that if you include non-SSL based content on an SSL page, users will get a pesky message in their browser about "insecure content" etc, which may put them off and create some needless doubt.

It may be quite difficult to split as you want - as you would need seperate root index.php CI files for each vhost to allow CI to route it correctly. You couldn't just set a vhost serving a directory such as application/controllers/private/ because CodeIgniter wouldn't know how to handle the request without some severe modification to it's core routing.

I would honestly just stick everything under an SSL vhost. Or, another option would be to set up two CI apps running from the same system/core CI folder... if that makes sense, but then sharing content such as libraries and models will become tedious.