单独的CodeIgniter公用文件夹 [英] Separate CodeIgniter public folder
问题描述
我一直在尝试更改CodeIgniter文件的结构,以使其更安全,更干净,但我无法使其正常工作。我想将用户可见的应用程序/系统和公共文件分开。
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
它可行,但我必须输入
example.com/public/index.php或example.com/public/controller
example.com/public/index.php or example.com/public/controller
我希望能够像example.com/controller这样输入基本URL。
I want to be able to just type the base URL like example.com/controller.
我该怎么做?
推荐答案
对于CodeIgniter 3.x,正确的处理方式(在2018年及之后)如下:
For CodeIgniter 3.x, the correct way to approach this (at this point in 2018 and after) is as follows:
- 在根文件夹中创建
公用
文件夹 - 移动您的index.php文件到该文件夹中(以防止歧义)
- 在已移动的index.php文件中,更改以下内容:
将
$ system_path
更改为../ system
更改
$ application_folde r
到../ application
- Make a
public
folder in your root folder - MOVE your index.php file into this folder (to prevent ambiguity)
- inside the moved index.php file, change the following:
change
$system_path
to../system
change
$application_folder
to../application
现在,我们需要一个 .htaccess
文件,但是默认情况下CI 3.x没有.htaccess文件,所以..如何从CI窃取它4.x默认情况下有哪个?您可以在这里找到它:
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
我建议您不要包括 SetEnv CI_ENVIRONMENT开发行
-而是在您的apache或NGinx .conf文件中定义此行,以便.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.
最后,您需要更新.conf文件,以便将 DOCUMENT_ROOT
设置为名为public的子文件夹(或您选择的命名方式) 。然后重新启动Apache / Nginx。
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.
-更新-
我发现我对CI 4中的 .htaccess
文件有问题,但是怀疑是我的系统出了问题。但是,这个简单的版本确实有效:
-- 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:
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|js|css|uploads|favicon.png)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]
这篇关于单独的CodeIgniter公用文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!