简体   繁体   English

单独的CodeIgniter公用文件夹

[英]Separate CodeIgniter public folder

I've been trying to change my CodeIgniter file structure to make it safer and cleaner but I can't get it to work. 我一直在尝试更改我的CodeIgniter文件结构,以使其更安全,更干净,但我无法使其正常工作。 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 example.com/public/index.php或example.com/public/controller

I want to be able to just type the base URL like example.com/controller. 我希望能够像example.com/controller这样输入基本URL。

How can I do it? 我该怎么做?

For CodeIgniter 3.x, the correct way to approach this (at this point in 2018 and after) is as follows: 对于CodeIgniter 3.x,正确的处理方式如下(在2018年及之后):

  • Make a public folder in your root folder 做一个public文件夹在你的根文件夹
  • MOVE your index.php file into this folder (to prevent ambiguity) 将您的index.php文件移动到此文件夹中(以防止歧义)
  • inside the moved index.php file, change the following: 在移动的index.php文件中,更改以下内容:

    change $system_path to ../system $system_path更改为../system

    change $application_folder to ../application $application_folder更改为../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? 现在,我们需要一个.htaccess文件,但是默认情况下CI 3.x没有.htaccess文件,所以..如何从默认具有CI.x的CI 4.x中窃取呢? You can find it here: 你可以在这里找到它:

https://github.com/bcit-ci/CodeIgniter4/blob/develop/public/.htaccess 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. 我建议您不要包括SetEnv CI_ENVIRONMENT development -而是在您的apache或NGinx .conf文件中定义此行,以便.htaccess文件可以在任何地方使用。

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). 最后,您需要更新.conf文件,以便将DOCUMENT_ROOT设置为名为public的子文件夹(或您选择的命名方式)。 Then restart Apache/Nginx. 然后重新启动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. -更新-我发现CI 4中的.htaccess文件存在问题,但是怀疑是我的系统出了问题。 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]

As per the CodeIgniter Installation Instructions ... 根据CodeIgniter安装说明 ...

/var/application/
/var/system/
/var/www/index.php

For the best security, both the system and any application folders should be placed above web root so that they are not directly accessible via a browser. 为了获得最佳安全性, 应将系统文件夹和所有应用程序文件夹都放置在Web根目录之上,以便不能通过浏览器直接访问它们。 By default, .htaccess files are included in each folder to help prevent direct access, but it is best to remove them from public access entirely in case the web server configuration changes or doesn't abide by the .htaccess. 默认情况下,每个文件夹中都包含.htaccess文件,以防止直接访问,但是最好在Web服务器配置更改或不遵守.htaccess的情况下,将其从公共访问权限中完全删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM