I've been trying to change my CodeIgniter file structure to make it safer and cleaner but I can't get it to work. I want to separate the application/system and the public files that are going to be visible to users.
- application
- system
- public
It works but I have to enter
example.com/public/index.php or example.com/public/controller
I want to be able to just type the base URL like example.com/controller.
How can I do it?
As per the CodeIgniter Installation Instructions...
For CodeIgniter 3.x, the correct way to approach this (at this point in 2018 and after) is as follows:
public
folder in your root folderchange
$system_path
to../system
change
$application_folder
to../application
Now, we need an
.htaccess
file, but CI 3.x doesn't have a .htaccess file by default, soo.. how about stealing it from CI 4.x which has it by default? You can find it here:https://github.com/bcit-ci/CodeIgniter4/blob/develop/public/.htaccess
I recommend you NOT include the line
SetEnv CI_ENVIRONMENT development
- instead, define this in your apache or NGinx .conf file so the .htaccess file can work anywhere.Finally, you'll meed to update your .conf file so that
DOCUMENT_ROOT
is set to the subfolder named public (or whatever you chose to name it). Then restart Apache/Nginx.That should give you the same results, and be much more secure.
-- UPDATE -- I found that I had problems with the
.htaccess
file from CI 4, however suspect it's my system that's the problem. This simple version did work however: